Detailed Explanation and Example Code of Python Dictionary

  • 2021-12-12 08:54:33
  • OfStack

What kind of directory dictionary looks like? What can you put in the dictionary? Access the dictionary content, modify the dictionary content, delete the dictionary data dictionary built-in function

Dictionary is the form of Python hash table data structure, which represents the mapping relationship, 1 to 1.

What does a dictionary look like

{} This is an empty dictionary. You can see that the dictionary is composed of two curly braces.
Looking at this {'a':1} , which contains a pair of data. ' a' Can be called a key, 1 Call it a value
This {'键1':'值1', '键2':'值2'} Every 1 pair of data

What can I put in the dictionary

The key in the dictionary is only 1, and only 1 exists in all contents in the dictionary, but the value can be repeated.
Keys can only be constant values, such as strings, numbers and tuples
Value can prevent any type of data at will

Access dictionary contents

Keys are placed in square brackets, such as


a = {'a':1,'b':2,'c':3}
print(a['a'])
print(a['c'])

Run results:

1
3

Modify dictionary contents


a = {'a':1,'b':2,'c':3}
a['a'] = 9
print(a['a'])

Run results:

9

Delete dictionary data

Delete key


a = {'a':1,'b':2,'c':3}
del a['a']
print(a['a'])

Run results:

---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
< ipython-input-3-740154434129 > in < module >
1 a = {'a':1,'b':2,'c':3}
2 del a['a']
---- > 3 print(a['a'])

KeyError: 'a'

Clear all data from the dictionary
I found that there is no data in the dictionary a


a = {'a':1,'b':2,'c':3}
a.clear()
print(a)

Run results:

{}

Delete dictionary
Discovery dictionary a is not defined


a = {'a':1,'b':2,'c':3}
del a
print(a)

Run results:

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
< ipython-input-5-af7009ccd2ed > in < module >
1 a = {'a':1,'b':2,'c':3}
2 del a
---- > 3 print(a)

NameError: name 'a' is not defined

Dictionary built-in function

1. dict.clear()
Delete all elements in the dictionary
2. dict.copy()
Returns a shallow copy of 1 dictionary
3. dict.fromkeys(seq[, val])
Create a new dictionary, using the elements in the sequence seq as the dictionary keys, and val as the initial values corresponding to all the dictionary keys
4. dict.get(key, default=None)
Returns the value of the specified key, or default value if the value is not in the dictionary
5. dict.has_key(key)
If the key returns true in the dictionary dict, otherwise it returns false
6. {'a':1}0
Returns an array of (key, value) tuples that can be traversed as a list
7. dict.keys()
Returns all keys of 1 dictionary as a list
8. dict.setdefault(key, default=None)
Similar to get (), but if the key does not exist in the dictionary, the key will be added and the value will be set to default
9. dict.update(dict2)
Update the key/value pair of dictionary dict2 to dict
10. dict.values()
Returns all the values in the dictionary as a list
11. pop(key[,default])
Delete the value corresponding to the dictionary given key key, and the return value is the deleted value. key value must be given. Otherwise, the default value is returned.
12. popitem()
Returns and deletes the last pair of keys and values in the dictionary.

The above is the use of Python dictionary details and examples of code details, more information about Python dictionary please pay attention to other related articles on this site!


Related articles: