Hello,
My first post here:
I am practicing, and I am trying to add an element to a list, which is into a dictionary. I realized that I could do it because my list was short, otherwise it would be impossible.
Exercise:
player_to_words = { “player1”: [“BLUE”, “TENNIS”, “EXIT”], “wordNerd”: [“EARTH”, “EYES”, “MACHINE”], “Lexi Con”: [“ERASER”, “BELLY”, “HUSKY”], “Prof Reader”: [“ZAP”, “COMA”, “PERIOD”] }
def play_word(player, word):
if player==“player1”:
player_to_words.update({“player1”: [“BLUE”, “TENNIS”, “EXIT”, word]})
print(play_word(“player1”, “NEW WORD”))
print(player_to_words)
Output:
(‘player1’, ‘NEW WORD’)
{‘player1’: [‘BLUE’, ‘TENNIS’, ‘EXIT’, ‘NEW WORD’], ‘wordNerd’: [‘EARTH’, ‘EYES’, ‘MACHINE’], ‘Lexi Con’: [‘ERASER’, ‘BELLY’, ‘HUSKY’], ‘Prof Reader’: [‘ZAP’, ‘COMA’, ‘PERIOD’]}
As you can see, I had to put the entire list into the function and add “word” to the list. but what if that list has a thousand elements?
How to do it without copy the list?
The solution player_to_words[“Player1”] = “NEW WORD” just overwrite everything.
Can someone help?