Home > Enterprise >  How to append a nested list?
How to append a nested list?

Time:12-17

today I started studying Python and have a question. I want to append the list itself, for example

a = [0, 1, 2]

want to make [0, 1, 2, [0, 1, 2]] by using function .append

a.append(a) gets result like [0, 1, 2, [...]]

Is it the correct answer? I don't know why the answer has [...] thing.

Also, what is the difference when I do this?

b = a.append(a)
print(b)
>> None

CodePudding user response:

  1. [1, 2, 3, [...]] is the correct answer. It is the way python represents recursive lists. Basically what that means is a = [1, 2, 3, [1, 2, 3, [1, 2, 3, [...]]]] infinite times.

  2. list.append returns None. a.append(a) therefore returns None and if you store that in b and print it, None is the correct answer

  • Related