Python dictionaries are indexed by keys. Strings and numbers can always be keys. Tuples can be too if they contain strings, numbers, or tuples. Dictionaries consist of key and value pairs.
An empty pair of braces creates an empty dictionary
empty_dictionary = {}
You can list a dictionary’s keys
my_dictionary = {'User 1': 'AwesomePassword', 'User 2': 'AnotherPassword'}
list(my_dictionary)
Sort it
my_dictionary = {'User 1': 'AwesomePassword', 'User 2': 'AnotherPassword'}
sorted(my_dictionary)
Delete an item
my_dictionary = {'User 1': 'AwesomePassword', 'User 2': 'AnotherPassword'}
del my_dictionary['User 1']
Search a list
my_dictionary = {'User 1': 'AwesomePassword', 'User 2': 'AnotherPassword'}
included = 'User 1' in my_dictionary
not_included = 'User 3894' not in my_dictionary
You can also create a dictionary with a comprehension
import string
alphabet_dictionary = {k: v for k, v in enumerate(string.ascii_lowercase)}
You can loop
import string
alphabet_dictionary = {k: v for k, v in enumerate(string.ascii_lowercase)}
for key, val in alphabet_dictionary.items():
print(key, val)
Loop in reverse
import string
alphabet_dictionary = {k: v for k, v in enumerate(string.ascii_lowercase)}
for key, val in reversed(alphabet_dictionary.items()):
print(key, val)
Loop in sorted order
import string
alphabet_dictionary = {k: v for k, v in enumerate(string.ascii_lowercase)}
for key, val in sorted(alphabet_dictionary.items()):
print(key, val)