Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the characteristics of Python dictionary

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the characteristics of Python dictionary". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the characteristics of the Python dictionary"?

Dictionaries are another variable container model and can store objects of any type.

Each key-value pair (key= > value) of the dictionary is separated by a colon (:), each pair is separated by a comma (,), and the entire dictionary is included in curly braces ({}) in the following format:

D = {key1: value1, key2: value2}

The key must be unique, but the value is not necessary.

Values can take any data type, but keys must be immutable, such as strings, numbers, or tuples.

A simple dictionary 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 values in the dictionary

Put the corresponding key in the familiar square brackets, as an example:

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

The output result of the above example:

Dict ['Name']: Zaradict [' Age']: 7

If you access the data with keys that are not in the dictionary, the output error is as follows:

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

The output result of the above example:

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

Modify the dictionary

The way to add new content to the dictionary is to add new key / value pairs and modify or delete the following examples of existing key / value pairs:

#! / usr/bin/python dict = {'Name':' Zara', 'Age': 7,' Class': 'First'}; dict [' Age'] = 8; # update existing entrydict ['School'] = "DPS School"; # Add new entry print "dict [' Age']:", dict ['Age']; print "dict [' School']:", dict ['School']

The output result of the above example:

Dict ['Age']: 8dict [' School']: DPS School

Delete dictionary element

Can delete a single element can also empty the dictionary, emptying only one operation.

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

#! / usr/bin/python#-*-coding: UTF-8-*-dict = {'Name':' Zara', 'Age': 7,' Class': 'First'}

Del dict ['Name']; # delete key is' Name' 'entry dict.clear (); # clear dictionary all entries del dict; # delete dictionary

Print "dict ['Age']:", dict [' Age']; print "dict ['School']:", dict [' School']

But this throws an exception because the dictionary no longer exists after using del:

Dict ['Age']: Traceback (most recent call last): File "test.py", line 8, in print "dict [' Age']:", dict ['Age']; TypeError:' type' object is unsubscriptable

Note: the del () method is also discussed later.

Characteristics of dictionary keys

Dictionary values can take any python object without restriction, either standard or user-defined, but not keys.

There are two important points to remember:

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

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

The output result of the above example:

Dict ['Name']: Manni

2) the key must be immutable, so it can be served as a number, string or tuple, so it is not possible to use a list, as shown in the following example:

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

The output result of the above example:

Traceback (most recent call last): File "test.py", line 3, in dict = {['Name']:' Zara', 'Age': 7}; TypeError: list objects are unhashable so far, I believe you have a better understanding of "what are the characteristics of Python dictionary". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report