Python

4 Python List – Appending to a list

Appending

Appending: Code

 #APPENDING TO LIST
print("Copying list from one to another")
my_favorite_foods = ['pizza', 'rice', 'cake']
friends_favorite_foods = my_favorite_foods[:]
#APPEND
print("Append new food items")
my_favorite_foods.append('fruits')
friends_favorite_foods.append('ice cream')
Print
print("My favorite foods are:")
print(my_favorite_foods)
print("\nMy friend's favorite foods are:")
print(friends_favorite_foods)

Results:

Append new food items
My favorite foods are:
['pizza', 'rice', 'cake', 'fruits', 'ice cream']
My friend's favorite foods are:
['pizza', 'rice', 'cake', 'fruits', 'ice cream']
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s