Python

4 Python List – Pointing to the same List

Pointing to a list

Assigning the a variable to a List – Code

BOTH VARIABLES POINTING TO THE SAME LIST
Any changes made to list appears same in both variables now
VAR1 = VAR2 - there is no : used in this case
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("My favorite foods are:")
print(my_favorite_foods)
print("\nMy friend's favorite foods are:")
print(friends_favorite_foods)

Results:

Copying list from one to another
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