-1

I am a bit confused about how I can add a variable to a list in python. I have looked at this post

and this was not very helpful. This is the code I have:

lst = [1, 2, 3]
        something = 4
        print(lst.append(something))

This for some reason returns none. Unsure if I am missing something. Any help would be grateful.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
AltBrian
  • 2,392
  • 9
  • 29
  • 58
  • 3
    Well, that's totally understandable because `list.append()` always return `None` and you are actually trying to print what this `append()` function is returning. – spectre009 Jun 28 '21 at 19:22
  • Does this answer your question? [Why does list.append() return None?](https://stackoverflow.com/questions/20016802/why-does-list-append-return-none) – Pranav Hosangadi Jun 28 '21 at 21:37
  • Does this answer your question? [Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?](https://stackoverflow.com/questions/11205254/why-do-these-list-operations-methods-clear-extend-reverse-append-sort) – Karl Knechtel Sep 15 '22 at 01:24

2 Answers2

-1
lst = [1, 2, 3]
something = 4
res = [int(something)]
print(lst + res)
AltBrian
  • 2,392
  • 9
  • 29
  • 58
-2
lst = [1, 2, 3]
something = 4
lst.append(something)
print(lst)
curium96
  • 19
  • 4