1

I'm trying to an item to my list:'pepperoni' by using append.

pizza_vege=['tomato sauce','mushrooms','pepper','cheese','garlic powder']
print(pizza_vege)
pizza_peper=pizza_vege.append('peperoni')
print(pizza_peper) #The result here should show the list of pizza_vege and add 'pepperoni to it'

The result shown is:

['tomato sauce', 'mushrooms', 'pepper', 'cheese', 'garlic powder']
None

I don't understand why 'print' returns 'None' in output. Isn't it supposed to join the two sequences??

thank you all !

InAFlash
  • 4,545
  • 7
  • 32
  • 53

2 Answers2

0

Change your code to

pizza_vege=['tomato sauce','mushrooms','pepper','cheese','garlic powder']
print(pizza_vege)
pizza_vege.append('peperoni')
print(pizza_vege)

Which yields

['tomato sauce', 'mushrooms', 'pepper', 'cheese', 'garlic powder']
['tomato sauce', 'mushrooms', 'pepper', 'cheese', 'garlic powder', 'peperoni']
Jan
  • 40,932
  • 8
  • 45
  • 77
0

In above, .append adds item to the original list and returns None. Instead if you want two different list for pizza_vege and pizza_peper then , you can try:

pizza_vege=['tomato sauce','mushrooms','pepper','cheese','garlic powder']
pizza_peper = pizza_vege + ['peperoni']

print(pizza_vege)
print(pizza_peper) #The result here should show the list of pizza_vege and add 'pepperoni to it'

Result:

['tomato sauce', 'mushrooms', 'pepper', 'cheese', 'garlic powder']
['tomato sauce', 'mushrooms', 'pepper', 'cheese', 'garlic powder', 'peperoni']
niraj
  • 15,852
  • 4
  • 32
  • 47