SoFunction
Updated on 2024-11-17

Dictionary operations and dictionary functions in python

dictionaries

dict_fruit = {'apple':'Apple','banana':'Banana','cherry':'Cherry','avocado':'Avocado','watermelon':'Watermelon'} 

Dictionary operations

# The way the dictionary is traversed
# Default traversal (traversal key)
for value in dict_fruit: 
  print(value) 
'''''
Iterated values:
watermelon
watermelon
cherry
avocado
avocado
''' 
# Use key traversal (same as default traversal)
for key in dict_fruit.keys(): 
  print(key) 
'''''
Iterated values:
watermelon
watermelon
cherry
avocado
avocado
''' 
# Use value traversal
for value in dict_fruit.values(): 
  print(value) 
'''''
Iterated values: the
Apple
avocado
Banana
Watermelon
Cherry
''' 
# Iterate using key,value
for key,value in dict_fruit.items(): 
  print(key+'--->'+value) 
'''''
Iterated values:.
avocado->avocado
apple--->apple
banana--->banana
cherry--->cherry
watermelon--->watermelon
''' 
#Creating a dictionary
# Use dict()
res = dict(brand = 'Brand',size='Size',color='Color') 
print(res,type(res)) 
''''' 
resin the end: 
{'size': 'Size', 'brand': 'Brand', 'color': 'Color'} <class 'dict'> 
''' 
# Use zip() and dict()
keys = ['1','2','3','4','5'] 
values = [1,2,3,4,5] 
res = dict(zip(keys,values)) 
print(res,type(res)) 
''''' 
resin the end: 
{'3': 3, '4': 4, '1': 1, '2': 2, '5': 5} <class 'dict'> 
''' 
# Derivatives of the dictionary
res = {k+' in Chinese is '+v for k,v in dict_fruit.items()} 
print(res) 
''''' 
resin the end: 
{'The Chinese word for watermelon is watermelon.', 'The Chinese word for avocado is avocado.', 'The Chinese word for banana is banana.', 'The Chinese word for cherry is cherry.', The Chinese word for 'apple' is 'pomegranate'.} 
''' 

Dictionary functions

#Empty the dictionary
test1 = {1:'1'} 
() 
print(test1) 
'''''
test1 result.
{}
'''' 
#Copy dictionary (copy into a new dictionary)
test2 = {2:'2'} 
test2_copy = () 
print(test2_copy) 
''''' 
test2in the end: 
{2: '2'} 
''' 
# Make a dictionary with the specified key and value
list_test = ['a','b','c'] 
test3 = {}.fromkeys(list_test,'ojbk') 
print(test3) 
''''' 
test3in the end: 
{'a': 'ojbk', 'b': 'ojbk', 'c': 'ojbk'} 
''' 
# Convert a dictionary into a secondary container (intermediate container)
res = dict_fruit.items() 
print(res,type(res)) 
''''' 
resin the end: 
dict_items([('avocado', 'Avocado'), ('apple', 'Apple'), ('banana', 'Banana'), ('watermelon', 'Watermelon'), ('cherry', 'Cherry')]) <class 'dict_items'> 
''' 
# Compose the dictionary key into a new container
res = dict_fruit.keys() 
print(res,type(res)) 
''''' 
resin the end: 
dict_keys(['watermelon', 'cherry', 'avocado', 'apple', 'banana']) <class 'dict_keys'> 
''' 
#Compose the values of the dictionary into a new container
res = dict_fruit.values() 
print(res,type(res)) 
''''' 
resin the end: 
dict_values(['Avocado', 'Banana', 'Cherry', 'Apple', 'Watermelon']) <class 'dict_values'> 
''' 
# Delete data from a dictionary based on key
test4 = {1:'1',2:'2',3:'3'} 
(2) 
print(test4) 
''''' 
test4in the end: 
{1: '1', 3: '3'} 
''' 
# Pop (delete) the data in the dictionary sequentially
test5 = {1:'1',2:'2',3:'3',4:'4',5:'5'} 
() 
print(test5) 
() 
print(test5) 
() 
print(test5) 
''''' 
test5依次in the end: 
{2: '2', 3: '3', 4: '4', 5: '5'} 
{3: '3', 4: '4', 5: '5'} 
{4: '4', 5: '5'} 
''' 
# Update the data in the dict (can be used to add new data when updating a non-existing key).
test6 = {'super':'Eric','ssuper':'Cbabe','sssuper':'Gogo','supreme':'wiz333'} 
#Update data
(super='Eric-LPL') 
print(test6) 
#Add data
(niceboy='Bigmao') 
print(test6) 
''''' 
test6依次in the end: 
{'ssuper': 'Cbabe', 'supreme': 'wiz333', 'sssuper': 'Gogo', 'super': 'Eric-LPL'} 
{'ssuper': 'Cbabe', 'supreme': 'wiz333', 'niceboy': 'Bigmao', 'sssuper': 'Gogo', 'super': 'Eric-LPL'} 
''' 
# Get the data in the dict (use key to get)
test7 = {1:'1',2:'2',3:'3',4:'4',5:'5'} 
res = (1) 
print(res,type(res)) 
'''''
test7 result.
1 <class 'str'> 
''' 
#Add data to dict (setdefault, can't be used to update data)
test8 = {1:'1',2:'2',3:'3',4:'4',5:'5'} 
(6,'6') 
print(test8) 
''''' 
test8in the end: 
{1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6'} 
''' 

summarize

The above is a small introduction to the dictionary operations and dictionary functions in python, I hope to help you, if you have any questions please leave me a message, I will reply to you in time. I would also like to thank you very much for your support of my website!