Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to append a nested list?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

  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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading