How to append a nested list?

Question:

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
Asked By: James Jang

||

Answers:

  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

Answered By: Clasherkasten
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.