Professional Documents
Culture Documents
To add a single key: value pair to a dictionary, we can use the syntax:
Dictionar_name [key] = value
Example : sensors = {"living room": 21, "kitchen": 23, "bedroom": 20,
"pantry" : 22}
Combining two lists: Let’s say we have two lists that we want to combine
into a dictionary, like a list of students and a list of their heights, in inches:
Example:
names = ['Jenny', 'Alexus', 'Sam', 'Grace']
heights = [61, 70, 67, 64]
value_of_key = name_of_dictionary[name_of_key]
If the key is not in the dictionary it will generate a key error, to avoid
this error we can use
If expresso in caffeine_level:
print(caffeine[“expresso”])
( II ) Use try/except :
caffeine_level["matcha"] = 30
try:
print(caffeine_level["matcha"])
except KeyError:
print("Unknown Caffeine Level")
Inside the parenthesis of .get, we write the name of the key of which
value we want to fetch.
In the .get ( ) method we can also set a default value of a key if the key is
not found in the dictionary.
For example, if we are going to fetch the value of “Dave age” and we don’t know if
Dave’s age is present in the data or not, we can give the user “ Dave age’s not
found”
2. TO DELETE A KEY :
.pop() works to delete items from a dictionary when you know the key
value.
.pop( ) can store also store the value of the key in the variable but it will
delete the key permanently from the dictionary.
If you do not know that a key exists that you are going to pop out you
can present or store a default value to variable.
3. GET ALL THE KEYS OUT OF THE DICTIONARY :
You can get both the keys and the values with the .items() method.
Like .keys() and .values(), it returns a dict_list object. Each element of
the dict_list returned by .items() is a tuple consisting of: (key, value)
(tuple = values of tuples can not be changed by any means)
Syntax : dict_name.items( )
It will convert a dictionary into a list with each key:value as a list item
Another Example :
The keys in the dictionary are Name, Address, and Age. Using append() method
can update the values for the keys in the dictionary.
my_dict = {"Name":[],"Address":[],"Age":[]};
my_dict["Name"].append("Guru")
my_dict["Address"].append("Mumbai")
my_dict["Age"].append(30)
print(my_dict) = {'Name': ['Guru'], 'Address': ['Mumbai'], 'Age': [30]}
Scrabble Solution :
letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
points = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 4, 1, 3, 10, 1, 1, 1, 1, 4, 4,
8, 4, 10]
letters_to_points = {key:value for key,value in zip(letters,points)}
letters_to_points[" "] = 0
def score_word(word):
word = word.upper()
score = 0
for char in word:
if char in letters_to_points:
score += letters_to_points[char]
return score
#print(score_word("brownie"))
player_to_words = {'Blue':["Earth","Eraser","Zap"],"Tennis":
["Eyes","Belly","Coma"],"Exit":["Machine","Husky","Period"]}
def play_word(player,word):
player_to_words[player].append(word)
return player_to_words
play_word('Blue',"Cricket")
play_word("Tennis","Zoology")
play_word("Exit","Kangaroo")
play_word('Blue',"Pokimon")
play_word('Tennis',"Cristanio Ronaldo")
play_word("Exit","Lional Messi ")
play_word("Blue","Zoopathology")
print(player_to_words)
players_to_points = {}
for players,words in player_to_words.items():
player_points = 0
for word in words:
player_points += score_word(word)
players_to_points[players]= player_points
print(players_to_points)