Python3 Dictionaries
Like a list, a dictionary is a collection of many values. But unlike indexes for lists, indexes for dictionaries can use many different data types, not just integers.
Indexes for dictionaries are called keys and a key with its associated value is called a key-value pair.
In code, a dictionary is typed with braces {}.
Eg. myCat = {'size': 'fat', 'color' :'gray', 'disposition':'loud'}
Keys are size, color,disposition and values are fat, gray and loud
Dictionaries vs Lists:
Unlike lists, items in dictionaries are unordered. While the order of items matters for determining whether two lists are the same, it does not matter in what order the key-value pairs are typed in a dictionary.
E.g:
animals =['cats', 'dogs', 'moose']
test = ['dogs', 'moose', 'cats']
animals == test
False
eggs={'name' :'jerry', 'species': 'cat', 'age:'10'}
ham = {'species':'cat', 'age:'10', 'name':'jerry'}
eggs ==ham
True
Because dictionaries are not ordered, they can't be sliced like lists..
There are three dictionary methods that will return list-like values of the dictionary's keys, values or both keys and values: keys(), values() and items().
Using the keys(), values() and items() methods, a for loop can iterate over the keys, values or key-value pairs in a dictionary.
Checking whether a key or value exists in a dictionary:
"in" and "not in" operators can check whether a value or key exists in a list.
E.g. 'name' in eggs.keys()
get() method:
Using get() method, you can pass two arguments, one value of key to retrieve and the second fallback value to return if the key does not exist.
E.g.:
picinicItems = {'apples':5 , 'cups':2}
'I am bringing' + str(picnicItems.get('cups',0)) +'cups
O/p: I am bringing 2 cups
'I am bringing' + str(picnicItems.get('eggs',0)) +'cups
O/p: I am bringing 0 cups
Pretty Printing:
You can import the pprint module into your programs, you'll have access to pprint() and pformat() functions that will "pretty print" a dictionary's values.
This is helpful when you want a cleaner display of the items in a dictionary than what print() provides.
import pprint
picinicItems = {'apples':5 , 'cups':2}
pprint.pprint(picnicItems)
Indexes for dictionaries are called keys and a key with its associated value is called a key-value pair.
In code, a dictionary is typed with braces {}.
Eg. myCat = {'size': 'fat', 'color' :'gray', 'disposition':'loud'}
Keys are size, color,disposition and values are fat, gray and loud
Dictionaries vs Lists:
Unlike lists, items in dictionaries are unordered. While the order of items matters for determining whether two lists are the same, it does not matter in what order the key-value pairs are typed in a dictionary.
E.g:
animals =['cats', 'dogs', 'moose']
test = ['dogs', 'moose', 'cats']
animals == test
False
eggs={'name' :'jerry', 'species': 'cat', 'age:'10'}
ham = {'species':'cat', 'age:'10', 'name':'jerry'}
eggs ==ham
True
Because dictionaries are not ordered, they can't be sliced like lists..
There are three dictionary methods that will return list-like values of the dictionary's keys, values or both keys and values: keys(), values() and items().
Using the keys(), values() and items() methods, a for loop can iterate over the keys, values or key-value pairs in a dictionary.
Checking whether a key or value exists in a dictionary:
"in" and "not in" operators can check whether a value or key exists in a list.
E.g. 'name' in eggs.keys()
get() method:
Using get() method, you can pass two arguments, one value of key to retrieve and the second fallback value to return if the key does not exist.
E.g.:
picinicItems = {'apples':5 , 'cups':2}
'I am bringing' + str(picnicItems.get('cups',0)) +'cups
O/p: I am bringing 2 cups
'I am bringing' + str(picnicItems.get('eggs',0)) +'cups
O/p: I am bringing 0 cups
Pretty Printing:
You can import the pprint module into your programs, you'll have access to pprint() and pformat() functions that will "pretty print" a dictionary's values.
This is helpful when you want a cleaner display of the items in a dictionary than what print() provides.
import pprint
picinicItems = {'apples':5 , 'cups':2}
pprint.pprint(picnicItems)
Comments
Post a Comment