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 !

rawwar
  • 4,834
  • 9
  • 32
  • 57

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
  • 42,290
  • 8
  • 54
  • 79
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
  • 17,498
  • 4
  • 33
  • 48