Re: lists and for loops

2011-08-18 Thread Peter Pearson
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lists and for loops

2011-08-18 Thread Tim Chase

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



--
http://mail.python.org/mailman/listinfo/python-list


Re: lists and for loops

2011-08-18 Thread Mark Niemczyk
Or, using list comprehension.

>>> numbers = [1, 2, 3, 4, 5]
>>> numbers = [n + 5 for n in numbers]
>>> numbers
[6, 7, 8, 9, 10]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lists and for loops

2011-08-17 Thread alex23
On Aug 18, 1:08 pm, 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

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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lists and for loops

2011-08-17 Thread Jack Trades
>
> 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
>
>
The n variable in the for loop refers to each value in the list, not the
reference to the slot that value is stored in.

To update the numbers list you would want something like this:

numbers = [1, 2, 3, 4, 5]
for n in range(len(numbers)):
  numbers[n] += 5
print numbers

Alternatively if you didn't need to update the numbers list you could make a
new list like this:

[n+5 for n in numbers]

-- 
Jack Trades 
Pointless Programming Blog 
-- 
http://mail.python.org/mailman/listinfo/python-list


lists and for loops

2011-08-17 Thread Emily Anne Moravec
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


-- 
http://mail.python.org/mailman/listinfo/python-list