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

Unable to append item in a list after slicing it python

list = [54, 44, 27, 79, 91, 41]

num_4 = list[4]
def fun_2():
    y = list[:2]
    z = y.append(num_4)
    print(z)
fun_2()

code above is giving me None output,
objective- first slice a list then append a integer to it.

>Solution :

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

This is because z does not contain the list but the y does.

List is mutable in python, which means,

y = list[:2]
z = y.append(num_4)

the above code will add num_4 to y and append() function returns none hence z will contain none.

So print y not z

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