Python Dictionary

The word reference is another variable holder display and can store any sort of item.

Each key an incentive for the word reference key=>value is part with a colon :, each key-esteem pair Intersected with a comma ,, the whole lexicon is encased in wavy supports {} in the accompanying organization:

d = {key1 : value1, key2 : value2 }

The key is commonly one of a kind. On the off chance that the last key-esteem pair is rehashed, the esteem shouldn't be exceptional.

>>>dict = {'a': 1, 'b': 2, 'b': '3'} >>> dict[ 'b'] '3' >>> dict {'a' : 1, 'b' : '3' }

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

A straightforward lexicon example:

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

You can also create a dictionary like this:

dict1 = { 'abc': 456 } dict2 = { ' abc': 123, 98.6 : 37 }

Access the value in the dictionary

Put the corresponding key into the familiar square bracket, as in the following example:


#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} print "dict['Name']: ", dict['Name'] print "dict['Age']: ", dict['Age']

The above example output:

dict['Name']: Zara
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/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} print "dict['Alice']: ", dict['Alice']

The above example output:

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

Modify dictionary

Adding new content to the dictionary is by adding new key/value pairs, modifying or deleting existing key/value pairs as follows:


#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} dict['Age'] = 8 # Update dict['School'] = "RUNOOB" # Add to print "dict['Age']: ", dict['Age'] print "dict['School']: ", dict['School']
The above example output:
dict['Age']: 8
dict['School']: RUNOOB

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/python # -*- coding: UTF-8 -*- dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} del dict['Name'] # The delete key is an entry for 'Name' dict.clear() # Clear all entries in the dictionary del dict # Delete dictionary print "dict['Age']: ", dict['Age'] print "dict['School']: ", dict['School']

But this will cause an exception, because the dictionary no longer exists after using del:

Traceback (most recent call last):
File "", line 8, in <module>
print "dict['Age']: ", dict['Age']
TypeError: 'type' object is unsubscriptable

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

Characteristics of dictionary keys

The dictionary value can take any python object without restriction, either as a standard object or as 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/python dict = {' Name': 'Zara' , 'Age' : 7, 'Name' : 'Manni' } print "dict['Name']: ", dict['Name']

The output of the above example:

dict['Name' ]: Manni

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


#!/usr/bin/python dict = {[ 'Name']: ' Zara', ' Age': 7} print "dict['Name']: ", dict['Name']

The above example output:

Traceback (most recent call last):
File "", line 3, in <module>
dict = {['Name']: 'Zara', 'Age': 7}
TypeError: list objects are unhashable

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