Reputation: 279
I want to create lists (named 'card') of two elements : 'NUMBER' and a number from the list 'numbers'. There will be as many cards as there are numbers in the list 'numbers'. Why do I get cards with the same number using that code ?
card=['NUMBER','']
numbers = [1,3,5]
All_cards=[]
for i in range(len(numbers)):
All_cards.append(card)
All_cards[i][1]=numbers[i]
print(All_cards)
What I get : [['NUMBER', 5], ['NUMBER', 5], ['NUMBER', 5]]
What I want : [['NUMBER', 1], ['NUMBER', 3], ['NUMBER', 5]]
Upvotes: 0
Views: 64
Reputation: 4229
Something like this?
numbers = [1, 3, 5]
all_cards = []
for number in numbers:
all_cards.append(['NUMBER', number])
print(all_cards)
You start with card = ['NUMBER', '']
, and every iteration you assign the current number to the second position of the same card
, so you end up with a list of equal elements, containing the last number.
You need to understand that if you define a list and append it to another list, it still refers to the original cards
list.
My example creates a new list in every iteration.
Upvotes: 1
Reputation: 25023
You have already excellent answers that solve your immediate problem (I suggest that you try to understand the mechanism of list comprehension, introduced in Mr Roseman answer, because it's extra handy in too many situations) but maybe you would like to understand why you had that strange result.
You had that strange result because you can change the contents of a list..
Eh? follow me, when you type at the prompt of the interpreter
>>> ['NUMBER', '']
['NUMBER', '']
>>>
you are creating a list, that is lost immediately (it's not exact, but) — if you make an assignment
>>> card = ['NUMBER', '']
>>>
you have created a list (rhs of =
) and assigned a name (card
, the lhs of =
) to this list.
Now you use a for
loop, and
All_cards.append(card)
doesn't mean that you are appending a copy of your list, you are appending (three times) the same list.All_cards[i][1]=numbers[i]
means that you're modifying, in all three passes in the loop, the same list.print(All_cards)
you see that all the three elements are the same, and that the second element of the inner list is the last numeric value that you have used inside the loop...May I add that this is, sort of, a rite of passage for every Python programmer? Bon voyage
Upvotes: 0
Reputation: 2794
If you add a print statement as last line of for loop, you would have got an idea what is happening.
It is as following:
[['NUMBER', 1]]
[['NUMBER', 3], ['NUMBER', 3]]
[['NUMBER', 5], ['NUMBER', 5], ['NUMBER', 5]]
[['NUMBER', 5], ['NUMBER', 5], ['NUMBER', 5]]
which is basically overriding the value.
You may modify your code like below to get expected output:
numbers = [1,3,5]
All_cards=[]
for i in xrange(len(numbers)):
# All_cards[i][1]=numbers[i]
card=['NUMBER', numbers[i]]
All_cards.append(card)
print(All_cards)
Output:
[['NUMBER', 1], ['NUMBER', 3], ['NUMBER', 5]]
Upvotes: 1
Reputation: 599490
You can do this with a simple list comprehension:
numbers = [1,3,5]
all_cards = [['NUMBER', i] for i in numbers]
Upvotes: 1