Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

How can I add Nothing to a list?

ruben.ciranni (4650 points)
8 14 31
in Programming in Python by (4.7k 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]
431 views
closed

3 Answers

Best answer
gabrimat (5630 points)
2 4 25
by (5.6k points)
selected by
Use sum instead of append and sum the empty list [] in case.
Lorenzo_Pellegrino (820 points)
2 6 10
by (820 points)
Can you explain further?
gabriel.bustamante (2540 points)
3 6 20
by (2.5k 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 (207920 points)
750 1267 2373
by (208k points)
+ and append are radically different

+ creates a new list

append changes the initial list

+= changes the initial list
James_F (6070 points)
10 14 47
by (6.1k points)
just do NOT append anything
Quellocinese (2700 points)
4 4 22
by (2.7k points)
have you thought about a try exception and just don't append if appending None