D
ICTIONARIES
thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"],
"type": {
"A1" : "21 Lac",
"A2" : "23Lac",
"A3" : "25Lac",
}
}
print(thisdict)
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
print(x) before the change
car.update({"year": 2020})
print(x) After the change
print("Yes, 'model' is one of the keys in the thisdict dictionary")
car.pop("year") The pop() method removes the item with the specified key name:
thisdict.popitem() The popitem() method removes the last inserted item
del car["model"] The del keyword removes the item with the specified key name:
print(car)
Loop Dictionaries
for x in car:
print(x) to key name
print(car[x]) to key value
For x in thisdict.values(): You can also use the values() method to return values of a dictionary:
For x in thisdict.keys(): You can use the keys() method to return the keys of a dictionary:
For x, y in thisdict.items(): Loop through both keys and values, by using the items() method:
Copy Dictionaries
mydict = car
mydict = car.copy()
mydict = dict(thisdict)
myfamily = {
"disc1" : thisdict,
"disc2" : {
"name" : "Linus",
"year" : 2011
}
}
print(myfamily)
ICTIONARIES
thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"],
"type": {
"A1" : "21 Lac",
"A2" : "23Lac",
"A3" : "25Lac",
}
}
print(thisdict)
{'brand': 'Ford', 'electric': False, 'year': 1964, 'colors': ['red', 'white', 'blue'], 'type': {'A1': '21 Lac', 'A2': '23Lac', 'A3': '25Lac'}}
print(type(thisdict))
<class 'dict'>
print(len(thisdict))
5
Access,Add,Change Itemscar = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
print(x) before the change
dict_keys(['brand', 'model', 'year'])
car["color"] = "white" If color key is not in it,it will be added in disc.car.update({"year": 2020})
print(x) After the change
dict_keys(['brand', 'model', 'year', 'color'])
print(type(x))
<class 'dict_keys'>
print(type(car.get("model")),car.get("model"))
<class 'str'> Mustang
print(type(car.values()),car.values())
<class 'dict_values'> dict_values(['Ford', 'Mustang', 2020, 'white'])
print(type(car.items()),car.items())
<class 'dict_items'> dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 2020), ('color', 'white')])
if "model" in car:print("Yes, 'model' is one of the keys in the thisdict dictionary")
Yes, 'model' is one of the keys in the thisdict dictionary
Remove Itemscar.pop("year") The pop() method removes the item with the specified key name:
thisdict.popitem() The popitem() method removes the last inserted item
del car["model"] The del keyword removes the item with the specified key name:
print(car)
{'brand': 'Ford', 'color': 'white'}
car.clear() The clear() method empties the dictionary:Loop Dictionaries
for x in car:
print(x) to key name
print(car[x]) to key value
brand
Ford
color
white
Ford
color
white
For x in thisdict.values(): You can also use the values() method to return values of a dictionary:
For x in thisdict.keys(): You can use the keys() method to return the keys of a dictionary:
For x, y in thisdict.items(): Loop through both keys and values, by using the items() method:
Copy Dictionaries
mydict = car
mydict = car.copy()
mydict = dict(thisdict)
myfamily = {
"disc1" : thisdict,
"disc2" : {
"name" : "Linus",
"year" : 2011
}
}
print(myfamily)
{'disc1': {'brand': 'Ford', 'electric': False, 'year': 1964, 'colors': ['red', 'white', 'blue']}, 'disc2': {'name': 'Linus', 'year': 2011}}
Comments
Post a Comment