0

I am new to python. I was learning the syntax of the for loop in python as:

for var in list_name: 
 # do something

I took a list:

list = [1,2,3,4,5,6,7,8,9]

I wanted to double each of its elements, so I run a loop as:

for i in list : 
 index = list.index(i) 
 list[index] = 2*i 
print(list) 

Then it prints:

 [16,2,12,4,10,6,14,8,18] 

I am not getting why it is printing like this?

Ashwini Chaudhary
252k60 gold badges478 silver badges519 bronze badges
asked Feb 9, 2014 at 10:10
3
  • 1
    Why do you alter the content on the list which you're looping over in the loop? That's recipe for all sorts of weird things like you're experiencing now. Create an empty list outside the loop and append the result of the computation to it or use list comprehension docs.python.org/2/tutorial/…. Commented Feb 9, 2014 at 10:20
  • thanx @Yaw, i used to do same in c , thats y i did it here... Commented Feb 9, 2014 at 10:28
  • To be honest, modifying just list elements while iterating is not problem at all (unless you do it in a function where people don't expect the list to be modified). Modifying the list itself is a completely different thing though (you don't want to do that). Commented Feb 9, 2014 at 10:33

5 Answers 5

6

Let's run through the first few iterations of your loop.

Iteration 1: i is 1.

index = list.index(i)
list[index] = 2*i

index is 0, and list[0] is set to 2*i. The list now looks like [2, 2, 3, 4, 5, 6, 7, 8, 9].

Iteration 2: i is 2.

index = list.index(i)

list.index(2) finds the first occurrence of 2 in the list, which is at index 0! There's more than one 2, and you're not selecting the right one.

list[index] = 2*i

You double the wrong element!

This happens again on iterations 4, 6, and 8.


If you want to double the elements of a list, the easiest way is to make a new list with a list comprehension:

l = [2*i for i in l]

If you need the indices of the elements in a for loop, the best way is usually to enumerate the list:

for i, item in enumerate(l):
 whatever()

Also, don't call your list list, or when you try to call the list function, you'll get a weird TypeError.

answered Feb 9, 2014 at 10:18
Sign up to request clarification or add additional context in comments.

Comments

3

Your code doesn't work as you expected, because list.index will return the first index of the element in the list. So, if the same element occurs more than once, it will not work as you expected.

The best way to double elements is to use list comprehension, like this

my_list = [item * 2 for item in my_list]

If you want to use a for loop, you can use enumerate, like this

for index, current_num in enumerate(my_list):
 my_list[index] = current_num * 2

This is ugly because we are modifying the list as we iterate it. So, don't do this. Instead you can do like this

for index in xrange(len(my_list)):
 my_list[index] *= 2
Maxime Lorant
36.4k19 gold badges91 silver badges97 bronze badges
answered Feb 9, 2014 at 10:14

1 Comment

You cannot change the length of a list while iterating over it, but it's completely fine to change the elements.
2

list.index(i) returns the first index in the list where i is found.

thelist = [1,2,3,4,5,6,7,8,9]
> index = thelist.index(1)
> index = 0
> thelist[0] = 2
> index = thelist.index(2)
> index = 0
> thelist[0] = 2*2 = 4
> index = thelist.index(3)
> index = 2
> thelist[2] = 2*3 = 6
> index = thelist.index(4)
> index = 0
> thelist[0] = 2*4 = 8
> index = thelist.index(5)
> index = 4
> thelist[4] = 2*5 = 10
> index = thelist.index(6)
> index = 2
> thelist[2] = 2*6 = 12
> index = thelist.index(7)
> index = 6
> thelist[6] = 2*7 = 14
> index = thelist.index(8)
> index = 0
> thelist[0] = 2*8 = 16
> index = thelist.index(9)
> index = 8
> thelist[8] = 2*9 = 18

and the rest of the elements will remain unaltered. Also, it is incorrect to use a keyword as a variable name. You should NOT use list as a variable name.

answered Feb 9, 2014 at 10:12

1 Comment

+1 For completeness include a working version as well.
0
print map(lambda x: x*2, my_list)
answered Feb 9, 2014 at 10:15

2 Comments

Obviously that prints what he wants but it doesn't answer his question: "I am not getting why it is printing like this?" and "Why is for loop not printing as expected in Python?".
Correct. I wasn't trying to answer those questions since we already have better and thorough answers. Just thought he might find it useful once he understand what he did wrong and look for other ways to achieve "what he wants".
0

Because you're altering the wrong element of your list each time. The syntax for i in list set the value of one element of the list, not an index.

In fact, you shouldn't altering the list you're working on but creating a new list. A correct way would be:

new_list = []
for i in list: 
 new_list.append(2*i)
# Or even, with a list comprehension
new_list = [i*2 for i in list]
print(new_list)
Ashwini Chaudhary
252k60 gold badges478 silver badges519 bronze badges
answered Feb 9, 2014 at 10:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.