-
Notifications
You must be signed in to change notification settings - Fork 1
/
dict.py
23 lines (20 loc) · 768 Bytes
/
dict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# syntax
dict = {'dude':'black', 'key': 'value'}
# syntax for ints as key
another_dict = {12:'some_value', 0: 'zero', 2: 'two'}
# print(another_dict[0])
# Dicts are un-ordered means
first = {'name': 'doodoo', 'gender': 'male'}
# second = {'gender':'male', 'name':'doodoo'}
# print(first == second) => returns true
# check keys existence using in and not in
# if 'name' in first:
# print("Yes. True")
# gets returned list of keys and values separately
store_keys = list(first.keys())
store_values = list(first.values())
store_items = list(first.items())
# store_items gets returned tuples.
# get() method returns default value if key doesn't exist
# setdefault() method can set a value if key doesn't exist
# prettyprint or pprint can print elegant dict values.