
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