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 is the dictionary method for Python to create an empty list

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

Share

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

The main content of this article is to explain "what is the dictionary method for Python to create an empty list". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the dictionary method for Python to create an empty list"!

There are several ways to create a dictionary in Python where keys and values are empty lists, but are there any differences between them? It needs to be verified by experiments, and the causes are analyzed. In this paper, two methods are tested and analyzed.

If you want to create a dictionary in Python where all the keys and values are lists, what should you do like this?

{1: [], 2: [], 3: [], 4: []}

Method 1, dictionary constructor

Use the dict constructor to generate (key,value) pairs

> key = [1,2,3,4] > a = dict ([(k, []) for k in key]) > a {1: [], 2: [], 3: [], 4: []}

Method 2, using fromkeys ()

Use the dictionary method fromkeys (key list, default value)

> key = [1,2,3,4] > b = {} .fromkeys (key, []) > b {1: [], 2: [], 3: [], 4: []}

Comparison of results

Is there any difference between the dictionaries generated by the two methods? Check it out:

> a [1] .append (1) > a {1: [1], 2: [], 3: [], 4: []} # only affects the corresponding key value list > > b [1] .append (1) > b {1: [1], 2: [1], 3: [1], 4: [1]} # all key value lists are affected

In the above results, it is found that the empty list generated by the fromkeys () method has added an element. It seems that they are the same object.

Cause analysis

As you can see from above, the empty list in the dictionary generated by the fromkeys () method is actually the same object. Why is this? Because the argument "[]" passed to the fromkeys () function is the same object, fromkeys () puts a shallow copy of this object in the dictionary.

If the object is mutable, there will be problems in subsequent operations. If the object that created the dictionary is mutable, you should avoid using fromkeys ()

At this point, I believe you have a deeper understanding of "what is the dictionary method for Python to create an empty list". 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