Python3 dictionary

The lexicon is another variable compartment demonstrate and can store any sort of article.

Each key estimation of the lexicon (key=>value) is part with a colon (:), isolated by a comma (, ). The whole lexicon is encased in wavy supports ({}) in the accompanying arrangement:

d = {key1 : value1, key2 : Value2 }

key must be special, however the esteem isn't vital.


esteem can take any information type, yet the keys must be permanent, for example, strings, numbers, or tuples.

A straightforward lexicon example:

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

You can likewise make a lexicon like this:

dict1 = { 'abc': 456 }; 

dict2 = { 'abc': 123, 98.6: 37 };

Access the incentive in the dictionary

Put the relating key in square sections, as in the accompanying example:


#!/usr/bin/python3 dict = {'Name': 'Welookups', 'Age': 7, 'Class': 'First'} print ("dict['Name']: ", dict['Name']) print ("dict['Age']: ", dict['Age'])

The output of the above example:

dict['Name' ]: Welookups
Dict['Age']: 7

If you access the data with a key that is not in the dictionary, the error will be output as follows:


#!/usr/bin/python3 dict = {'Name': 'Welookups', 'Age': 7, 'Class': 'First'}; print ("dict['Alice']: ", dict['Alice'])

The output of the above example:

Traceback (most recent call last):
File "test.py", line 5, in <module>
print ("dict['Alice']: ", dict['Alice'])
KeyError: 'Alice'

dict['Age'] = 8; # Update Age dict['School'] = "Web tutorial" # add information print ("dict['Age']: ", dict['Age']) print ("dict['School']: ", dict['School'])
The above example output:
dict['Age']: 8
dict['School']: Web tutorial

Delete dictionary elements

The ability to delete a single element can also clear the dictionary, emptying only one operation.

Show delete a dictionary with the del command, as in the following example:


#!/usr/bin/python3 dict = {'Name': 'Welookups', 'Age': 7, 'Class': 'First'} del dict['Name'] # Delete key 'Name' dict.clear() # Clear dictionary del dict # Delete dictionary print ("dict['Age']: ", dict['Age']) print ("dict['School']: ", dict['School'])

This will raise an exception because the dictionary no longer exists after performing the del operation:

Traceback (most recent call last):
File "test.py", line 9, in <module>
print ("dict['Age']: ", dict['Age'])
TypeError: 'type' object is not subscriptable

Note:del() The method will also be discussed later.。

Characteristics of dictionary keys

The dictionary value can be any python object, either a standard object or a user-defined one, but not a key.

Two important points to remember:

1) The same key is not allowed to appear twice. If the same key is assigned twice when creating, the latter value will be remembered, as in the following example:


#!/usr/bin/python3 dict = {'Name': 'Welookups', 'Age': 7, 'Name': 'Little Web'} print ("dict['Name']: ", dict['Name'])

The output of the above example:

dict['Name' ]: small Web

2) The key must be immutable, so it can be played with numbers, strings or tuples, but not with the list, as in the following example:


#!/usr/bin/python3 dict = {[ 'Name']: ' Welookups', ' Age': 7} print ("dict['Name']: ", dict['Name'] )

The output of the above example:

Traceback (most recent call last):
File "test.py", line 3, in <module>
dict = {['Name']: 'Welookups', 'Age': 7}
TypeError: unhashable type: 'list'

Dictionary built-in function & method

The Python dictionary contains the following built-in functions:

>>> dict = {'Name': 'Welookups', 'Age': 7, 'Class': 'First'}
>>> type(dict)
<class 'dict'>
serial numberfunction and descriptioninstance
Calculate the number of dictionary elements, which is the total number of keys。
>>> dict = {'Name': 'Welookups', 'Age': 7, 'Class': 'First'}
>>> len(dict)
Output dictionary, represented by a printable string。
>>> dict = {'Name': 'Welookups', 'Age': 7, 'Class': 'First'}
>>> str(dict)
"{'Name': 'Welookups', 'Class': 'First', 'Age': 7}"
Returns the input variable type, if the variable is a dictionary, returns the dictionary type。

welookups is optimized for learning.© welookups 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.