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?
-
1Why 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/….Yaw Boakye– Yaw Boakye2014年02月09日 10:20:30 +00:00Commented Feb 9, 2014 at 10:20
-
thanx @Yaw, i used to do same in c , thats y i did it here...Saurabh Jain– Saurabh Jain2014年02月09日 10:28:04 +00:00Commented 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).ThiefMaster– ThiefMaster2014年02月09日 10:33:59 +00:00Commented Feb 9, 2014 at 10:33
5 Answers 5
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.
Comments
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
1 Comment
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.
1 Comment
print map(lambda x: x*2, my_list)
2 Comments
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)