Repository navigation
Expand file tree
/
Copy pathdictionary.py
More file actions
executable file
·67 lines (55 loc) · 2.15 KB
/
Copy pathdictionary.py
File metadata and controls
executable file
·67 lines (55 loc) · 2.15 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python
## dictionary.py
## List and String are 'Sequences' in Python
## Dictionaries are 'Mappings' in Python.
## Mappings are nothing but collection of objects sorted by a key.
## They will be represented by a key and value.
## Note, you cannot sort or index a Dictionary because they are stored as a Mapping. You wil have to explicitly call them out in order.
## Checkout, ordereddict to maintain order in a dictionary.
my_dict = {'key1': 'value1', 'key2': 'value2'}
print(my_dict['key2'])
## Mixing Data Types in a dictionary
my_map2 = {'k1': 123, 'k2': 3.45, 'k3': 'Chait'}
print(my_map2['k3'])
## Nested Operations on the Dict.
print("First Alphabet : %s" %my_map2['k3'][0])
print("Upper Case: %s" %my_map2['k3'].upper())
print("Reverse: %r" %my_map2['k3'][::-1])
## Changing Value associated with that Key.
print("Before Change : %s" %my_map2['k1'] )
my_map2['k1'] = my_map2['k1'] - 120
print("After Change: %s" %my_map2['k1'])
my_map2['k1'] = 16
print("After Change: %s" %my_map2['k1'])
## Performing List Operations on Dicts.
print(my_map2['k3'][0]) # Print index elemnt 0 in dict k3
print(my_map2['k3'].upper()) # Print Upper case
print(my_map2['k3'][::-1]) ## reverse value of k3
## Quick Syntax for the above
## Self Addidtion and re-assigning
my_map2['k1'] += 100
print("After Quick Syntax: %s" %my_map2['k1'])
## Creating an Empty Dict
d = {}
d['animal'] = 'Dog'
d['answer'] = 421
print(d)
## Nested Dictionaries.
d = {'k1': { 'nestkey': { 'subnestkey': "Value1"}}}
print(d['k1']) # Printing Value of outer Key
print(d['k1']['nestkey']) # Printing Value on Inner Key
print(d['k1']['nestkey']['subnestkey']) # Printing Value of Innermost Key.
## You can run more Methods on nested Dicts as well.
print(d['k1']['nestkey']['subnestkey']).upper()
## Built in Dictionary Methods.
newd = {}
newd['k1'] = 1
newd['k2'] = 2
newd['k3'] = 3
print(newd)
print(newd.keys()) # will return only keys
print(newd.values()) # will return only values
newd.values().reverse() ## Using reverse method. Applying on list and replacing it.
print(newd.values())
print(newd.values()[::-1]) # will return reverse values
print(newd.items()) # will return Key, Value pairs, also called Tuples.