+ 7
[Solved] This is how the "for loop" assigns values from iterator to the variables in each iteration.
arr = ['b', 'c', 'd'] x = {1:'a'} n = 1 for x[n] in arr: n += 1 print(x) Output: {1: 'b', 2: 'c', 3: 'd'}
10 Answers
+ 5
How loop works?
Ex :
for i in range( 1,5) :
print( i )
Output:
1
2
3
4
In each iteration, from range() iterator, the next() value is assigning into temporary variable i like
In first iteration : i = 1 # by iterator.next() ,
Next itearation : i = 2
next : i = 3
next : i = 4
in your code, you are using dict variable as temporary variable. so dictionary updates value of key already exist. if not, then it adds value into dictionary with key 'n' value...
hope it helps...
+ 7
x is dictionary
You know that, x[key] = value assigns value to dictionary x with at 'key'.
Same this, is happening in loop there..
Hope it helps..
+ 5
Thank you Jayakrishna🇮🇳 and Sandeep and everyone else contributed.
I understood a key feature of for loop which I didn't paid attention to it till now.
Thank you so much for your detailed answers and clarifications. 👌🙏🌹❤
+ 4
Many people forget that the "for ... in ..." loop is a generator, it does what you are interested in. 😎 Check it out:
arr = ['b', 'c', 'd']
x = {1:'a'}
n = 2
print(x[n] for x[n] in arr)
print()
print(list(x[n] for x[n] in arr))
print()
for x[n] in arr:
print(f'x[{n}] =',x[n])
n += 1
print()
print('Dictionary x =',x)
+ 3
i think this is how it works...
---------
x[i] is getting changed in the for loop, as mentioned by jayakrishna but how? as we don't write any assignment statement...!
(earlier i also got confused,)
In case of normal for loop with temporary variable i, i value get changed in every iteration.
for i in arr:
# do something
In above case, i value will be 'b' in first iteration, in second iteration i value will be 'c' and so on.
- here notice that we are temporarily storing the value in variable i.
---------------------------------------------------------
Same thing is happening in your code, we iterate through the list and temporarily store the value in x[n], therefore in first iteration of 'for loop' ...
x[n] = 'b'
x[1] = 'b' # n is 1
here we stored the value given by for loop in x[1] which had a value of 'a' but now it contains b.
- we modified the value in x dictionary in order to store the temporary value
+ 3
Part 2 (continue, just for clarification)
Now, inside 'for loop' n got incremented. Hence in second iteration,
x[n] = 'c' # for loop gives us 'c' value
x[2] = 'c'
we stored the item given by 'for loop' in x[2], since x[2] don't exist ... it will create a key 2 and assign it value 'c'.
Same things happens in third iteration.
This is happening because we are using dictionary as a temporary variable. We simply ignore this when we use 'i' as a temporary variable because we don't use 'i' outside 'for loop'
+ 3
Korkunç the Terrible Frusciante
Yes. It's a kind implicit assignment.
First, in each iteration,
x[n] = iterator.next() method is called which will return next value from itearator into x[n] in loop.
Siavash Kardar Tehran
You're welcome..
Hope it helps.
+ 2
Dear Jayakrishna🇮🇳 I don't see any assignment in the for loop.
Because of this I'm wondering why it is assigning values to the dictionary!
In the loop it is just incrementing the n, where is the assessment?!
+ 2
Jayakrishna🇮🇳 Can we essentially call a for loop a series of iterated implicit assignments? Could I use the word "implicit" if I wanted to ask what OP asked?
+ 2
Thank you Jayakrishna🇮🇳 :-)