- Dictionaries have keys and values
- Dictionaries keys are like indexes in lists and tuples
- Dictionaries keys do not have to be integers
- Dictionaries keys are immutable and unique
- Dictionaries values are like elements in lists and tuples
- Dictionaries values are mutable
- Dictionaries values can be duplicates
- Dictionaries can also contain many dictionaries -> called nested dictionaries
- Creating Dictionaries
- using {}
- using dict()
- Nesting Dictionaries
- Returning keys
- Returning Values
- Returning Values using keys
- Adding values to Dictionaries
- Removing Values from Dictionaries
- Removing values with Specified keys (pop)
- Removing values with Specified keys (del)
- Removing last value of Dictionaries
- Changeing Values in Dictionaries
- Dictionaries and Loop
- Returning Keys
- Returning Values (First Way)
- Returning Values (Second Way)
- Returning Keys and Values
- Dictionaries and if Statements
- Dictionaries and try/except
- Copying Dictionaries
- using copy()
- using dict()
- Updating Dictionaries
- Dictionary Length
- Emtying Dictionaries
- Deleting Dictionaries
- using {}
- using dict()
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
my_dict
your_dict = dict(GingerandRosa = 2013 , Enemy = 2014, ObviousChild = 2014)
your_dict
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
your_dict = {"Ginger & Rosa": 2013 , "Enemy" : 2014, "Obvious Child": 2014}
both_dict = { "my_dict" : my_dict, "your_dict" : your_dict }
both_dict
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
my_dict.keys()
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
my_dict.values()
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
my_dict["Agatha Christie"]
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
my_dict['Georges Simenon'] = 570
my_dict
- Removing values with Specified keys (pop)
- Removing values with Specified keys (del)
- Removing last value of Dictionaries
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
my_dict.pop("William Shakespeare")
my_dict
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
del my_dict["William Shakespeare"]
my_dict
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
my_dict.popitem()
my_dict
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723,
"Danielle Steel": 179 , "Harold Robbins": 23 }
my_dict['Georges Simenon'] = 2
my_dict
- Returning Keys
- Returning Values (First Way)
- Returning Values (Second Way)
- Returning Keys and Values
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
for key in my_dict:
print(key)
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
for value in my_dict.values():
print(value)
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
for value in my_dict:
print(my_dict[value])
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
for key, value in my_dict.items():
print(key,value)
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
if "William Shakespeare" in my_dict:
print("Yes")
else:
print("No")
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
try:
print(my_dict = ["John"])
except:
print("It is incorrect")
- using copy()
- using dict()
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
your_dict = my_dict.copy()
your_dict
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
your_dict = dict(my_dict)
your_dict
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
new_dict = { "William Shakespeare": 44 , "Agatha Christie": 88 , "Barbara Cartland": 777}
my_dict.update(new_dict)
my_dict
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
len(my_dict)
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
my_dict.clear()
my_dict
- When you delete the dictionary -> your dictionary no longer exist
- If you call your dictionary -> it causes an error
- Error -> NameError: name 'my_dict' is not defined
my_dict = { "William Shakespeare": 42 , "Agatha Christie": 85 , "Barbara Cartland": 723}
del my_dict
my_dict