6.6 KiB
6.6 KiB
In [ ]:
# Make a dictionary with {} and : to signify a key and a value
my_dict = {'key1':'value1','key2':'value2'}In [ ]:
# Call values by their key
my_dict['key2']In [ ]:
my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}In [ ]:
# Let's call items from the dictionary
my_dict['key3']In [ ]:
# Can call an index on that value
my_dict['key3'][0]In [ ]:
# Can then even call methods on that value
my_dict['key3'][0].upper()In [ ]:
my_dict['key1']In [ ]:
# Subtract 123 from the value
my_dict['key1'] = my_dict['key1'] - 123In [ ]:
#Check
my_dict['key1']In [ ]:
# Set the object equal to itself minus 123
my_dict['key1'] -= 123
my_dict['key1']In [ ]:
# Create a new dictionary
d = {}In [ ]:
# Create a new key through assignment
d['animal'] = 'Dog'In [ ]:
# Can do this with any object
d['answer'] = 42In [ ]:
#Show
dIn [ ]:
# Dictionary nested inside a dictionary nested inside a dictionary
d = {'key1':{'nestkey':{'subnestkey':'value'}}}In [ ]:
# Keep calling the keys
d['key1']['nestkey']['subnestkey']In [ ]:
# Create a typical dictionary
d = {'key1':1,'key2':2,'key3':3}In [ ]:
# Method to return a list of all keys
d.keys()In [ ]:
# Method to grab all values
d.values()In [ ]:
# Method to return tuples of all items (we'll learn about tuples soon)
d.items()In [ ]: