-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionaries.py
More file actions
34 lines (23 loc) · 973 Bytes
/
dictionaries.py
File metadata and controls
34 lines (23 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import json
import random
faves = {"tv genre": "sci-fi", "guilty pleasure": "tik tok", "color": "pink"}
print(faves)
print(json.dumps(faves, indent=4)) #prints in json, indents, and makes easier to read. make sure to import json
print(faves["color"])
faves["scent"] = "floral" # adds new key/value pair
print(faves)
print(dir(faves))
print(list(faves.keys()))
print(list(faves.values()))
for k, v, in faves.items():
print(k, v)
var3 = {
1: [random.randint(0, 10), random.randint(0, 10), random.randint(0, 10)],
2: [random.randint(0, 10), random.randint(0, 10), random.randint(0, 10)],
3: [random.randint(0, 10), random.randint(0, 10), random.randint(0, 10)]
}
print(json.dumps(var3, indent=4)) # Pretty prints the dictionary var3
for key, value in var3.items(): # Iterates over the key-value pairs in var3
print(key, value)
for obj in value: # Iterates over the elements in each sublist
print(obj) # Prints each element