-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdictionaries.py
More file actions
94 lines (70 loc) · 1.96 KB
/
dictionaries.py
File metadata and controls
94 lines (70 loc) · 1.96 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# A dictionary is a collection which is unordered, changeable and indexed.
# and it doesn't allow duplicate members.
# let us define some variables...
name = 'Abdul'
surname = 'Mtoro III'
age = 22
special_powers = 'Mind Reading'
# Even more variables...
name_1 = 'Zainab'
surname_1 = 'Adam'
age_1 = 22
special_powers_1 = 'Weather Manipulation'
# Create a single dictionary..
mutant = {
'first_name':name,
'last_name' :surname,
'age':age,
'special_powers': special_powers
}
# returning a dictionary with it's type..
print(mutant, type(mutant))
# use a constructor..
cons_mutant = dict(name=name,surname = surname)
# get the keys..
print(cons_mutant['name'])
# or using get method
print(cons_mutant.get('surname'))
# Add key/value
cons_mutant['Origin'] = 'Xavier School of Gifted Youngsters'
# Get all the keys from a dictionary
print(cons_mutant.keys())
# Get dictionary items
print(cons_mutant.items())
# copying a dictionary and printing all the items..
clone_mutant = cons_mutant.copy()
print(clone_mutant.items())
# Adding a new key to a clone dictionary
clone_mutant['skin Colour'] = 'Blue Coloured'
print(clone_mutant.keys())
# Remove item
del(cons_mutant['Origin'])
print(cons_mutant.keys())
# or using pop method
clone_mutant.pop('skin Colour')
print(clone_mutant.keys())
# Clearing a dictionary
print(clone_mutant.clear())
# Get length of a dictionary
print(len(clone_mutant))
# Collection of Dictionaries
mutants = [
# First dictionary
{
'name':name,
'age':age,
'Special Powers':special_powers
},
# Second dictionary
{
'name':name_1,
'age':age_1,
'Special Powers':special_powers_1
},
]
# Printing multiple dictionaries
print(mutants)
# print specified keys from collections of dictionaries
print(mutants[1]['name'])
# Get length of items in the second dictionary
print(len(mutants[1]))