-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06DS_Dict.py
146 lines (94 loc) · 2.15 KB
/
06DS_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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
#Data Stuctures - Dictionaries
#Dictionary - collection, unordered, changeable/ mutable, indexed, curly bracket with key:value pairs
#List Mutable, Indexed, Unorderd, collection, square bracket
#Dictionary Mutable, key-value paired, Unorderd, collection, curly brackets
#Tuples - unmutatble, collection, ordered, round bracket
stud={'rollno':1, 'name': "Vikas"}
stud['rollno']
stud={'name':'Vikas', 'class':'MBA'}
stud
car = { 'brand':'Honda', 'model': 'Jazz', 'year' : 2017}
car
#access
car['brand']
car.get('brand')
#change
car['year'] = 2018
car.get('year')
car
#list all Keys
for i in car:
print(i)
#list all values
for i in car:
print(car[i])
car.values()
for i in car.values() :
print(i)
car.items()
([('brand', 'Honda'), ('model', 'Jazz'), ('year', 2018)])
#key - value
for j, i in car.items() :
print(i, j)
Honda brand
Jazz model
2018 year
#check if key exist
if 'model' in car:
print('Model key exists')
#length
len(car)
#add items
car['color1']= 'black'
car
#remove items
pop_element =car.pop('color1')
pop_element
car
#removes last inserted item ie color here
car.popitem()
car
#remove item with specified key name
del car['brand']
car
#delete dictionary
del car
car
#empty dictionary
car = { 'brand':'Honda', 'model': 'Jazz', 'year' : 2017}
car
car.clear()
car
car['brand'] ='hyundia'
car
#copy dictionary
car = { 'brand':'Honda', 'model': 'Jazz', 'year' : 2017}
car
yourcar = car.copy()
yourcar
car['color']="Black"
yourcar
car
#method2
hiscar = car
hiscar
#new dictionary
newcar = dict(brand ='Honda', model ='Jazz', year=2017)
newcar
#round bracket, no quotes in keys, equal to symbol
#methods
#clear, copy, fromkeys, get, items, pop, popitem, setdefault, update, values
car(['brand','year'])
student= {('name', 1):'Vikas',('name', 2):'Arun', ('Rank',2):'Pass'}
student[('name',2)]
student = {(1, 'Vikas'): 'Noida', (1,'Pooja'): 'Ghaziabad'}
type(student)
student[(1,'Vikas')]
student[(2,'Aman')] = 'Delhi'
student
student.keys()
student.values()
student.items()
#create a dictionary of phone : first,name -> mobile no
#uses : create dataframe, key-value pairs