One thing first: No, this is not a question about why
someThing = [[]] * 10
someThing[0] = 1
gives [1,1,1,1,1,1,1,1,1,1].
My question is a bit more tricky: I do the folling:
x, y, z = ([[]] for i in range(3))
Now I assign a value:
x[0] = 1 # This works fine
And now another one:
x[1] = 2 # This does not work anymore
It gives me an IndexError: list assignment index out of range.
How can I make this work? As you can see, I want an empty list that contains 3 other lists to which i can append values.
Thanks in advance!
4 Answers 4
I want an empty list that contains 3 other lists to which i can append values.
Your problem correctly stated is: "I want a list of three empty lists" (something cannot be empty and contain things at the same time).
If you want that, why are you doing it in such a convoluted way? What's wrong with the straightforward approach:
empty_list = [[], [], []]
If you have such a list, when you do empty_list[0] = 1
, what you end up with is:
empty_list = [1, [], []]
Instead, you should do empty_list[0].append(1)
, which will give you:
empty_list = [[1], [], []]
If you want to create an arbitrary number of nested lists:
empty_list = []
for i in range(0, 15):
empty_list.append([])
-
good question! the answer is simple: my 3 lists in one list were only an example. originally, i wanted to put 15 lists into a list. of course, your way is working, but it looks somewhat weird if i implement it that way. i just wanted to learn if there is an advanced way of doing this. but thanks ;)PKlumpp– PKlumpp04/29/2014 08:29:24Commented Apr 29, 2014 at 8:29
Since when
someThing = [] * 10
someThing[0] = 1
gives [1,1,1,1,1,1,1,1,1,1]
? It's an index error (you can't assign at a nonexisting index), because actually []*10 == []
. Your code does exactly the same (I mean an index error). This line
x, y, z = ([[]] for i in range(3))
is short (or not) for
x, y, z = [[]], [[]], [[]]
so x[1]
is an index error.
-
sry, you are right, i forgot brackets there. Yes i know the index does not exist, that is what the error message already told me. but how do i solve my problem?PKlumpp– PKlumpp04/29/2014 08:27:24Commented Apr 29, 2014 at 8:27
-
@ZerO
I want an empty list that contains 3 other lists
I don't understand - it can be empty and contain something at the same time. If you want to append something to a list, then use.append
. See Burhan's answer.freakish– freakish04/29/2014 09:04:20Commented Apr 29, 2014 at 9:04
Maybe something like this could work.
lists = [[] for i in range(3)]
The output would be:
[[],[],[]]
If you would like to access/add elements, you could:
lists[0].append(1)
lists[0].append(2)
lists[1].append('a')
lists[1].append('b')
The output would be:
[[1, 2], ['a', 'b'], []]
-
yes, this does work. but i need a multi-assignment ;)PKlumpp– PKlumpp04/29/2014 11:35:54Commented Apr 29, 2014 at 11:35
Comparing the output of:
for i in [[]] * 3:
print id(i)
with:
for i in ([[]] for j in range(3)):
print id(i)
may help answer your finding.
-
are you kidding me? Did you even read my question? I think i clearly showed that this is NOT the problem -.-PKlumpp– PKlumpp04/30/2014 06:56:24Commented Apr 30, 2014 at 6:56
-
@ZerO: Did you even try the above code? I see no bearing on your question apart from the subtle but significant difference shown by the above. You have a 10 in your first example, and a 3 in your second. Why? If you already knew the difference between multiplication of lists versus generators, then
print i
andprint i[0]
andprint i[1]
as well and see why you are getting anIndexError
. I am on SO thinking I like to learn and help, and I may be wrong in my answer.vapace– vapace04/30/2014 16:32:18Commented Apr 30, 2014 at 16:32
x[0] = 1
actually does? It replaces one of your nested lists with the number 1. If you want to append values, you should useappend
, not item assignment.