-5
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.

buran
  • 13,682
  • 10
  • 36
  • 61

1 Answers1

1

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

Himanshu Kawale
  • 389
  • 2
  • 11