1

I have executed three different for loop operations in Jupyter notebook. Code is given below

First for loop

b = {}
temp_1 = []
for x in range(0,4):
    y = [4,5]
    temp_1.append(x*y)
b[x] = temp_1
print("\n First for loop output \n",b)

Second for loop

b = {}
temp_1 = []
for x in range(0,4):
    for y in range(4,6):
        temp_1.append(x*y)
    b[x] = temp_1
print("\n 2nd for loop output \n",b)

Third for loop

b = {}
temp_1 = []
for x in [0,1,2,3]:
    for y in [4,5]:
        temp_1.append(x*y)
    b[x] = temp_1
print("\n 3rd for loop output \n",b)

Output of First for loop output

{0: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]], 1: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]], 2: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]], 3: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]]}

2nd for loop output

{0: [0, 0, 4, 5, 8, 10, 12, 15], 1: [0, 0, 4, 5, 8, 10, 12, 15], 2: [0, 0, 4, 5, 8, 10, 12, 15], 3: [0, 0, 4, 5, 8, 10, 12, 15]}

3rd for loop output

{0: [0, 0, 4, 5, 8, 10, 12, 15], 1: [0, 0, 4, 5, 8, 10, 12, 15], 2: [0, 0, 4, 5, 8, 10, 12, 15], 3: [0, 0, 4, 5, 8, 10, 12, 15]}

Q1: I am beginner to Python. I have been doing simple coding in MATLAB C and Arduino UNO controller. Literally, I am failing to understand how for loop works in Python. Particularly, what is happening in First for loop and second and third.

Q2: I wanted the output something like {0:[0,0], 1:[4,5],2:[8,10],3:[12,15]}. Could you please help me with proper code which can fetch me this output.

3
  • In that first loop, what do you get and what would you expect? Can you explain each line in separation? Commented Jul 18, 2018 at 4:45
  • It appears as if you want a dictionary with a nested list inside. I'll see what I can do. Commented Jul 18, 2018 at 5:02
  • In First loop, interestingly, y=[4,5] is just repeated the number of times as x. Example, look at the first string { 0: [ [4,5], [4,5,4,5,], [4,5,4,5,4,5] ] }. First value in first string has one [4,5] and second value [4,5,4,5] has two [4,5]. Why it is like this? Commented Jul 18, 2018 at 8:51

3 Answers 3

5

Try this:

b = {}
for x in [0,1,2,3]:
    temp_1 = []
    for y in [4,5]:
        temp_1.append(x*y)
    b[x] = temp_1
print("\n 3rd for loop output \n",b)

In your first loop, you are multiplying a list [4,5] which is not what you want because it is not element-wise. Read http://thepythonguru.com/python-lists/ to see how operators work on Python lists. The second and third loops are the same; you just need to clear your temp_1 list in every iteration on x.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot. It perfectly worked. Thanks for providing information about new source. I have one more doubt: how indexing of elements in a string works in python. For example, s= [23, 34, 54, 89]. My question is how to know the index position or location of particular element '89' in s. Could you please, give me some code to it.
In First loop, interestingly, y=[4,5] is just repeated the number of times as x. Example, look at the first string { 0: [ [4,5], [4,5,4,5,], [4,5,4,5,4,5] ] }. First value in first string has one [4,5] and second value [4,5,4,5] has two [4,5]. Why it is like this?
It's very simple: [23, 34, 54, 89].index(89)
Massoud Hosseinali could you please tell me. How to plot {0: [0, 0], 1: [4, 5], 2: [8, 10], 3: [12, 15]} over [0,10]. I mean, x axis will have two values x1=0, x2=10. How to plot 0:{0,0} on to this. That is, y1=0,y2=0. Similarly, how to plot second string values 1:[4,5] that is y1=4,y2=5. Please, help me with your code.
1

For the first for loop, you are multiplying a list with a number and appending to another list(temp_1). Multiplying a list with a number n is equivalent to repeating the elements of the list n times. Since you are appending to another list after every iteration of x, the value: x*y will get appended on a new line. When you do b[x] = temp_1, it is repeating the value of temp_1 for every iteration of x. Hence a 2D list.

The second and third cases of for loops are essentially the same because range(0, 4) will generate a list [0, 1, 2, 3] as you've done manually in case 3. When y takes on values from range(4,6), we go through each value individually and multiply by x (which takes on individual values from range(0,4)). Hence, we keep multiplying individual elements and get temp_1 = [0, 0, 4, 5, 8, 10, 12, 15]. Similar to the previous case where you used b[x] = temp_1, this value repeats for each iteration of x.

To get the result you mentioned above, you can use the method mentioned by Massoud Hosseinali. Hope this helps!

3 Comments

Thanks a lot. Looks like you are the right person to answer my question and enlighten my basics about for loop in Python. In C language: for x in [0,1,2,3] means, x will assume any one of the values in list in a iteration. I hope! I am right. But, In Python, for x in [0,1,2,3], something different from C is hapenning.
Actually in C language, you would use it as such: int x; for(x = 0; x<4; x++){code}
For example, consider first loop in my above example. After understanding your explanation. The first string in output should be something like b= {0:[[ ]], 1:..}. Strangely, it is like this b= {0: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]], 1:...}. That is, when x is 0, y=[4,5] is repeated for three times which is total range of x. I did not understand here. I hope! you got my question. This totally confused me.
1

For your first for loop, y = [4,5] which is a list as a whole is repeated by x times.

>>> y=[4,5]
>>> 0*y
[]
>>> 2*y
[4, 5, 4, 5]
>>> 3*y
[4, 5, 4, 5, 4, 5]

For your second and third for loop, 'for y in [4,5]:' and 'for y in range(4,6):' are the same and so you get the same result. Obviously the result is wrong since all the values are the same for all keys in the dictionary. This is because you put 'temp_1 = []' out of for loop. PLEASE NOTE that 'b[x] = temp_1' means that b[x] and temp_1 point to the same object since Python deals with objects by passing references, so every time 'temp_1' is updated, b[0] ~ b[x] are updated to the same value. You might use built-in function id() to check the address of the objects in memory.

>>> x = {0: 'apple'}
>>> y = x
>>> y
{0: 'apple'}
>>> id(x)
55111152
>>> id(y)
55111152

6 Comments

In above three for loop codes, b[x] = temp_1 is put 8 spaces. Actually, it is 4 spaces only. Sorry for my mistake. For example, consider first loop in my above example. After understanding your explanation. The first string in output should be something like b= {0:[[ ]], 1:..}. Strangely, it is like this b= {0: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]], 1:...}. That is, when x is 0, y=[4,5] is repeated for three times which is total range of x. Because, value of x is still 0. But, I did not understand here. I hope! you got my question. This totally confused me.
I understand you don't get the point why all the values following : are the same, right? They are all "[[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]]".
No. Why it is repeating three times in every string. For example, when x=0, the first string in output should be {0:[[ ]], 1:[[ ],[4,5]],..}. In first string itself, that it is repeated three times. It should have been zero times. In second string (x=2), it should have repeated two times only. This is my question.
I don't think you have understand my comment: This is because you put 'temp_1 = []' out of for loop. PLEASE NOTE that 'b[x] = temp_1' means that b[x] and temp_1 point to the same object since Python deals with objects by passing references, so every time 'temp_1' is updated, b[0] ~ b[x] are updated to the same value. You need to put statement 'temp_1 = []' in the for loop like this: 'for x in [0,1,2,3]: temp_1 = []', then every time you update b[x], b[0] ~ b[x-1] would not be updated.
Yes! I am slowly getting the point and what is Python. I am more used to C and thinking in that way. But, it is different here. Your explanation helped me to understood the fundamental of Python in a great way. Thanks a lot. So, every time, I update temp_1, it is automatically updating the previous b(x). That's way it is repeating. I got it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.