How can I add Nothing to a list?

ruben.ciranni (4650 points)
7 13 31
asked Dec 11, 2021 in Programming in Python by ruben.ciranni (4,650 points)
Is there a way to append a blank item to a list? I don't want to append the value None, but really nothing.

Ex.

>>> a = [1,2,3,4]

>>> a.append(None)

>>> a

#what I would like to happen

[1,2,3,4]

#what happens

[1,2,3,4,None]
131 views

3 Answers

Best answer
gabrimat (5630 points)
2 3 25
answered Dec 11, 2021 by gabrimat (5,630 points)
selected Dec 11, 2021 by ruben.ciranni
Use sum instead of append and sum the empty list [] in case.
Lorenzo_Pellegrino (770 points)
2 5 10
commented Dec 11, 2021 by Lorenzo_Pellegrino (770 points)
Can you explain further?
gabriel.bustamante (2540 points)
3 5 20
commented Dec 11, 2021 by gabriel.bustamante (2,540 points)
>>> a = [1,2,3,4]

>>> b  = []

>>> a + b

[1, 2, 3, 4]

Keep in mind that "+" and .append() are not always interchangeable
andrea.sterbini (172780 points)
514 935 1789
commented Dec 12, 2021 by andrea.sterbini (172,780 points)
+ and append are radically different

+ creates a new list

append changes the initial list

+= changes the initial list
James_F (4750 points)
7 14 37
answered Dec 11, 2021 by James_F (4,750 points)
just do NOT append anything
Quellocinese (2700 points)
2 4 22
answered Dec 11, 2021 by Quellocinese (2,700 points)
have you thought about a try exception and just don't append if appending None