Skip to content

Instantly share code, notes, and snippets.

@lfalanga
Last active February 9, 2020 03:36
Show Gist options
  • Save lfalanga/8b7ee1f8f6562ec305f9f0aaad11d8e8 to your computer and use it in GitHub Desktop.
Save lfalanga/8b7ee1f8f6562ec305f9f0aaad11d8e8 to your computer and use it in GitHub Desktop.
# Example 1: Accessing dictionaries values by key (could be either a string or a number)
# Assigning a dictionary with three key-value pairs to residents:
residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}
print residents['Puffin'] # Prints Puffin's room number
# Your code here!
print residents['Sloth']
print residents['Burmese Python']
# Example 2: Adding values to a new dictionary
menu = {} # Empty dictionary
menu['Chicken Alfredo'] = 14.50 # Adding new key-value pair
print menu['Chicken Alfredo']
# Your code here: Add some dish-price pairs to menu!
menu['Milanesa Napolitana'] = 1.50
menu['Suprema Maryland'] = 2.50
menu['Risotto de Hongos'] = 3.50
print "There are " + str(len(menu)) + " items on the menu."
print menu
# Example 3: Deleting and Changing dictionary item
# key - animal_name : value - location
zoo_animals = { 'Unicorn' : 'Cotton Candy House',
'Sloth' : 'Rainforest Exhibit',
'Bengal Tiger' : 'Jungle House',
'Atlantic Puffin' : 'Arctic Exhibit',
'Rockhopper Penguin' : 'Arctic Exhibit'}
# A dictionary (or list) declaration may break across multiple lines
# Removing the 'Unicorn' entry. (Unicorns are incredibly expensive.)
del zoo_animals['Unicorn']
# Your code here!
del zoo_animals['Sloth']
del zoo_animals['Bengal Tiger']
zoo_animals['Rockhopper Penguin'] = 'Foo'
print zoo_animals
# Example 4: Working with various operations at dictionaries
inventory = {
'gold' : 500,
'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']
}
# Example 5: Adding a key 'burlap bag' and assigning a list to it
inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']
# Sorting the list found under the key 'pouch'
inventory['pouch'].sort()
# Your code here
inventory['pocket'] = ['seashell', 'strange berry', 'lint']
inventory['backpack'].sort()
inventory['backpack'].remove('dagger')
inventory['gold'] += 50
# Example 6: Assigning a dictionary with three key-value pairs to residents:
residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}
print residents['Puffin']
# Example 7: Empty dictionary
menu = {}
# Adding new key-value pair
menu['Chicken Alfredo'] = 14.50
print menu['Chicken Alfredo']
# Exaample 8: Getting dictionary value by key name
del dict_name[key_name]
# Example 9: Dictionary with multiple types of data inside
my_dict = {
"fish": ["c", "a", "r", "p"],
"cash": -4483,
"luck": "good"
}
print my_dict["fish"][0]
# Example 10: Given a dictionary object, prints the values inside of it using for loop
webster = {
"Aardvark" : "A star of a popular children's cartoon show.",
"Baa" : "The sound a goat makes.",
"Carpet": "Goes on the floor.",
"Dab": "A small amount."
}
for key in webster:
print webster[key]
# Example 10.1: Printing out dictionary keys and value on the same line
d = {
"name": "Eric",
"age": 26
}
for key in d:
print key, d[key]
# Example 11: Accesing a list from a dictionary of lists
animal_sounds = {
"cat": ["meow", "purr"],
"dog": ["woof", "bark"],
"fox": [],
}
print animal_sounds["cat"]
# Example 12: Print dictionary items using '.items()' function
def foo():
d = {
"Name": "Guido",
"Age": 56,
"BDFL": True
}
print d.items()
# => [('BDFL', True), ('Age', 56), ('Name', 'Guido')]
foo()
# Example 13: Printing out dictionary keys and values separately
my_dict = {
"Book name": "Las cabezas trocadas",
"Chapters": 9,
"Great book": True
}
print my_dict.keys()
print my_dict.values()
# Example 14: Joining two dictionaries
diccionario1 = {'valor1': 1, 'valor2': 2}
diccionario2 = {'valor3': 3, 'valor4': 4, 'valor5': 5}
diccionario2.update(diccionario1)
print(diccionario2)
# Example 14: Dictionary to string formatting
dict_button_properties = {
"activebackground": "green",
"activeforeground": "yellow",
"background": "black",
"foreground": "red"
}
a = ', '.join(["%s= '%s'" % (key, value) for (key, value) in dict_button_properties.items()])
print(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment