# Two methods to create a dictionary
sample_dictionary = {
'name_key1' : 12,
'name_key2' : 'value'
}
sample_dictionary_2 = dict([('name', 'value'), ('price', 8.9), ('calories', 250), ('combo', ['french frise', 'Coke'])])This is notes based on freeCodeCamp
What is Dictionary
- Definition
- Dictionaries: Dictionaries are built-in data structures that store collections of key-value pairs. Keys need to be immutable data types.
Access a value from a dictionary dictionary[key]
How to change/update value in a dictionary dictionary_name['key_name'] = 'new_value_name'
How to retrieve a value associated with a key get()
What is a view object?
What returns a view object with all the key-value pairs in the dictionary?
What removes all of the key-value pairs from the dictionary?
products = {
'Laptop': 990,
'Smartphone': 600,
'Tablet': 250,
'Headphones': 70,
}
for product in products.items():
print(product)('Laptop', 990)
('Smartphone', 600)
('Tablet', 250)
('Headphones', 70)
What is List
What is Set
Compare Dictionrary, List and Set
dictionrary = collection of key-value pairs list = ordered collection, allows duplicates set = unordered collection, does not allow duplicates
this_is_a_list = [1, 2, 2, 3] this_is_a_set = {1, 2, 3}
medical_record = ['name_1', 'name_2']You can create an dictionary in a list
customer_infos = [
{
'patient_id': 'P1001',
'first_name': 'Dann',
'age': 22,
'gender': 'Male',
'purchase': ['milk toast', 'bread'],
'last_visit_id': 'V2505',
},
{
'patient_id': 'P1002',
'first_name': 'Emilia',
'age': 21,
'gender': 'Female',
'purchase': ['toast', 'white bread'],
'last_visit_id': 'V2505',
},
]