0

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!

asked Apr 29, 2014 at 8:17
2
  • 1
    Did you look at what x[0] = 1 actually does? It replaces one of your nested lists with the number 1. If you want to append values, you should use append, not item assignment. Commented Apr 29, 2014 at 8:19
  • yes you are right. but this is only an example. in my implementation, i do use append ;) Commented Apr 29, 2014 at 8:31

4 Answers 4

3

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([])
answered Apr 29, 2014 at 8:24
1
  • 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 ;) Commented Apr 29, 2014 at 8:29
3

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.

answered Apr 29, 2014 at 8:19
2
  • 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? Commented 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. Commented Apr 29, 2014 at 9:04
2

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'], []]
answered Apr 29, 2014 at 10:47
1
  • yes, this does work. but i need a multi-assignment ;) Commented Apr 29, 2014 at 11:35
0

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.

answered Apr 29, 2014 at 19:31
2
  • are you kidding me? Did you even read my question? I think i clearly showed that this is NOT the problem -.- Commented 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 and print i[0] and print i[1] as well and see why you are getting an IndexError. I am on SO thinking I like to learn and help, and I may be wrong in my answer. Commented Apr 30, 2014 at 16:32

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.