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.
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).
Answer. No, each key in a dictionary should be unique. You can't have two keys with the same value.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With