Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python's 'for ... in' work differently on a list of values vs. a list of dictionaries?

I'm wondering about some details of how for ... in works in Python.

My understanding is for var in iterable on each iteration creates a variable, var, bound to the current value of iterable. So, if you do for c in cows; c = cows[whatever], but changing c within the loop does not affect the original value. However, it seems to work differently if you're assigning a value to a dictionary key.

cows=[0,1,2,3,4,5]
for c in cows:
  c+=2

#cows is now the same - [0,1,2,3,4,5]

cows=[{'cow':0},{'cow':1},{'cow':2},{'cow':3},{'cow':4},{'cow':5}]
for c in cows:
  c['cow']+=2

# cows is now [{'cow': 2}, {'cow': 3}, {'cow': 4}, {'cow': 5}, {'cow': 6}, {'cow': 7}
#so, it's changed the original, unlike the previous example

I see one can use enumerate to make the first example work, too, but that's a different story, I guess.

cows=[0,1,2,3,4,5]
for i,c in enumerate(cows):
  cows[i]+=1

# cows is now [1, 2, 3, 4, 5, 6]

Why does it affect the original list values in the second example but not the first?

[edit]

Thanks for the answers. I was looking at this from a PHP point of view, where you can use the & symbol in foreach to specify whether you are operating on a reference to or a copy of the iterable. I see now that the real difference is a basic detail of how python works regarding immutable objects.

like image 857
JAL Avatar asked May 28 '10 04:05

JAL


People also ask

Are lookups faster with dictionaries or lists in Python?

Lookups are faster in dictionaries because Python implements them using hash tables. If we explain the difference by Big O concepts, dictionaries have constant time complexity, O(1) while lists have linear time complexity, O(n).

Can values Be Same in dictionary Python?

Answer. No, each key in a dictionary should be unique. You can't have two keys with the same value.

What is the difference between a dictionary and a list in Python?

Both of these are tools used in the Python language, but there is a crucial difference between List and Dictionary in Python. A list refers to a collection of various index value pairs like that in the case of an array in C++. A dictionary refers to a hashed structure of various pairs of keys and values.

How are dictionary different from the list?

But what's the difference between lists and dictionaries? A list is an ordered sequence of objects, whereas dictionaries are unordered sets. However, the main difference is that items in dictionaries are accessed via keys and not via their position.


1 Answers

It helps to picture what happens to the reference held by c in each iteration:

[ 0, 1, 2, 3, 4, 5 ]
  ^
  |
  c

c holds a reference pointing to the first element in the list. When you do c += 2 (i.e., c = c + 2, the temporary variable c is reassigned a new value. This new value is 2, and c is rebound to this new value. The original list is left alone.

[ 0, 1, 2, 3, 4, 5 ]

  c -> 2

Now, in the dictionary case, here's what c is bound to during the first iteration:

[ {'cow':0}, {'cow':1}, {'cow':2}, {'cow':3}, {'cow':4}, {'cow':5} ]
     ^
     |
     c

Here, c points to the dictionary object {'cow':0}. When you do c['cow'] += 2 (i.e., c['cow'] = c['cow'] + 2), the dictionary object itself is changed, as c is not rebound to an unrelated object. That is, c still points to that first dictionary object.

[ {'cow':2}, {'cow':1}, {'cow':2}, {'cow':3}, {'cow':4}, {'cow':5} ]
     ^
     |
     c
like image 87
Santa Avatar answered Oct 21 '22 09:10

Santa