Thanks for helping me! My question been answered in the comments, by @juanpa.arrivillaga . Key point of my question is about how python know "I am trying to assign the first element to i and the second element to string".
I am learning python, but how does [string for i, string in enumerate(a)]
work really troubles me.
Here are 2 example code I write while learning python
a = ['a','b','c', 'd']
d = [string for string in enumerate(a)]
print (d)
it would return '[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]'. However, if I write the following code
a = ['a','b','c', 'd']
c = [string for i, string in enumerate(a)]
print(c)
it would return ['a', 'b', 'c', 'd']
In my understanding, string in enumerate(a)
would translated to the "enumerated object" like position inside RAM. and I do not tell python what the i is (I did not tell python i
is the index or string is the element). So, the code would be explained as "list["enumerated object" iterate inside "i"]", but since I did not tell python what i is, it will run into error. However, it returned a list of elements from enumerate.
Where do python learned from my code the string
means element
inside enumerate, and i
means the index?