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

does .appened affect the order of arguments in list?

I ran the below code:

names = ["Carol", "Albert", "Ben", "Donna"]
names.append("Eugenia")
print(sorted(names))

and I got:

['Albert', 'Ben', 'Carol', 'Donna', 'Eugenia']

.append should add the element at the end of list, but order of elements got changed, why the result isn’t ['Carol', 'Albert', 'Ben', 'Donna', 'Eugenia']?

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

>Solution :

After appending "Eugenia", you’re sorting the list before printing (with sorted). Append merely places a new element at the end of the list.

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