Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11733 > unrolled thread
| Started by | Emily Anne Moravec <moravec@stolaf.edu> |
|---|---|
| First post | 2011-08-17 20:08 -0700 |
| Last post | 2011-08-18 16:43 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
lists and for loops Emily Anne Moravec <moravec@stolaf.edu> - 2011-08-17 20:08 -0700
Re: lists and for loops alex23 <wuwei23@gmail.com> - 2011-08-17 20:23 -0700
Re: lists and for loops Mark Niemczyk <prahamark@gmail.com> - 2011-08-18 05:22 -0700
Re: lists and for loops Tim Chase <python.list@tim.thechases.com> - 2011-08-18 07:45 -0500
Re: lists and for loops Peter Pearson <ppearson@nowhere.invalid> - 2011-08-18 16:43 +0000
| From | Emily Anne Moravec <moravec@stolaf.edu> |
|---|---|
| Date | 2011-08-17 20:08 -0700 |
| Subject | lists and for loops |
| Message-ID | <a47fc507-4e45-4a1d-b133-10a6bd2095f3@c14g2000yqk.googlegroups.com> |
I want to add 5 to each element of a list by using a for loop.
Why doesn't this work?
numbers = [1, 2, 3, 4, 5]
for n in numbers:
n = n + 5
print numbers
[toc] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2011-08-17 20:23 -0700 |
| Message-ID | <b599dd9d-0533-44ef-9bb5-8ce2db4760e8@m5g2000prh.googlegroups.com> |
| In reply to | #11733 |
On Aug 18, 1:08 pm, Emily Anne Moravec <mora...@stolaf.edu> wrote:
> I want to add 5 to each element of a list by using a for loop.
>
> Why doesn't this work?
>
> numbers = [1, 2, 3, 4, 5]
> for n in numbers:
> n = n + 5
> print numbers
As the for loop steps through numbers, it assigns each integer value
to the label n. What n holds is a number, _not_ a reference to the
number in the list. So when you reassign n to hold n+5, you're
pointing n at a new number, not modifying the original number being
referred to.
So what you need is a reference to the position of the number in the
list, so you can reassign the value that's held there. The common
pattern is to use enumerate, which lets you step through a list giving
you both an reference (index) & a value:
numbers = [1, 2, 3, 4, 5]
for i, n in enumerate(numbers):
numbers[i] = n + 5
Here you're reassigning each list element to hold its old value plus
5.
Hope this helps.
[toc] | [prev] | [next] | [standalone]
| From | Mark Niemczyk <prahamark@gmail.com> |
|---|---|
| Date | 2011-08-18 05:22 -0700 |
| Message-ID | <8307c651-e4c1-4d28-9f2a-bf0513f21eb8@glegroupsg2000goo.googlegroups.com> |
| In reply to | #11736 |
Or, using list comprehension. >>> numbers = [1, 2, 3, 4, 5] >>> numbers = [n + 5 for n in numbers] >>> numbers [6, 7, 8, 9, 10]
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2011-08-18 07:45 -0500 |
| Message-ID | <mailman.154.1313671526.27778.python-list@python.org> |
| In reply to | #11745 |
On 08/18/2011 07:22 AM, Mark Niemczyk wrote: > Or, using list comprehension. > >>>> numbers = [1, 2, 3, 4, 5] >>>> numbers = [n + 5 for n in numbers] >>>> numbers > [6, 7, 8, 9, 10] Or, if you want it in-place: numbers[:] = [n+5 for n in numbers] which makes a difference if you have another reference to numbers: >>> numbers = [1,2,3,4,5] >>> digits = numbers >>> numbers = [n+5 for n in numbers] >>> numbers, digits ([6, 7, 8, 9, 10], [1, 2, 3, 4, 5]) >>> numbers = [1,2,3,4,5] >>> digits = numbers >>> numbers[:] = [n+5 for n in numbers] >>> numbers, digits ([6, 7, 8, 9, 10], [6, 7, 8, 9, 10]) -tkc
[toc] | [prev] | [next] | [standalone]
| From | Peter Pearson <ppearson@nowhere.invalid> |
|---|---|
| Date | 2011-08-18 16:43 +0000 |
| Message-ID | <9b4tp8FdrgU1@mid.individual.net> |
| In reply to | #11733 |
On Wed, 17 Aug 2011 20:08:23 -0700 (PDT), Emily Anne Moravec wrote: > I want to add 5 to each element of a list by using a for loop. > > Why doesn't this work? > > numbers = [1, 2, 3, 4, 5] > for n in numbers: > n = n + 5 > print numbers Because integers are immutable. You cannot turn 1 into 6. Contrast this behavior with lists, which *are* mutable: >>> numbers = [[1],[2],[3],[4],[5]] >>> for n in numbers: ... n[0]= n[0] + 5 ... >>> numbers [[6], [7], [8], [9], [10]] For practical purposes, I'm sure you'll find other responders' excellent posts to be of more immediate use, but keeping mutability in mind helps. -- To email me, substitute nowhere->spamcop, invalid->net.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web