Python dictionaries and the use of lists some of the places to note _python

Source: Internet
Author: User
Tags data structures

Python has three very useful data structures, lists, tuples, and dictionaries, and tuples are immutable, lists can save any type of Python object, and can be arbitrarily extended without size limits, the dictionary is a key-value type of key-value mapping, can store any Python object, A dictionary can be nested, and the value can be a dictionary tuple or a dictionary

Here's a Python dictionary and list trap. Some of the features of Python, if you don't understand these features, can cause some bugs that are hard to find

Let's introduce these features

All use of lists and dictionaries in Python is just a reference to the original object instead of creating a new object such as the following code:

>>> info = dict (name= ' cold ', blog= ' www.linuxzen.com ') # Create dictionary {' name ': ' Cold ', ' blog ': ' www.linuxzen.com '}
>>> Info2 = Info   # Assign to Info2
>>> info2[' name ' = ' Cold night '
>>> info
> >> info2
{' blog ': ' www.linuxzen.com ', ' name ': ' Cold Night '}
>>> info
{' blog ': ' Www.linuxzen.com ', ' name ': ' Cold Night '}
>>> names = [' Cold ', ' Night ', ' Linuxzen ']
>>> Names2 = names
>>> names2.append (' Cold Night ')
>>> names
[' Cold ', ' Night ', ' Linuxzen ', ' Cold Night ']
>>> names2
[' Cold ', ' Night ', ' Linuxzen ', ' Cold Night ']

You see that if you reassign a list or a dictionary to another variable, you don't get the desired result, we change one while the other is changing at the same time, and if we want to keep a snapshot, it's obvious that we're not doing what we want, and there's a common use because we know that ordinary variables are passed to function, a function that changes internally does not affect external variables, so what about lists and dictionaries? Let's look at the following code, we create a function, the dictionary adds a key and a value, and the list adds an element to the tail.

>>> def add_something (info):
...   If type (info) = = dict:
...       info[' msg '] = ' Hello, ' + info[' name '
...   Elif Type (info) = = List:
...       Info.append (' Add to the list ') ... 
>>> info = {' name ': ' Cold ', ' blog ': ' www.linuxzen.com '}
>>> add_something (info)
>> > Info
{' blog ': ' www.linuxzen.com ', ' msg ': ' Hello,cold ', ' name ': ' Cold '}
>>> names = [' Cold ', ' Night ', ' linuxzen.com ']
>>> add_something (names)
>>> names
[' Cold ', ' Night ', ' Linuxzen.com ', ' Add to the list ']

As the code is obviously not what we want, if this list/dictionary is used in just one place, it may not happen. If we need the same list elsewhere, if you don't know this feature, it's going to be a very hard to find bug when it's not what we want. How do we avoid it? We can make a copy of the list/dictionary, not a simple reference

>>> names = [' Cold ', ' Night ', ' linuxzen.com ']
>>> names2 = names[:]
>>> Names2.append (' Cold Night ')
>>> names
[' Cold ', ' Night ', ' linuxzen.com ']
>>> names2
[' Cold ', ' Night ', ' linuxzen.com ', ' Cold Night ']
>>> info = {' name ': ' Cold Night ', ' blog ': ' Www.linuxzen.com '}
>>> Info2 = info.copy ()
>>> info2[' name '] = ' cold '
>>> Info
{' blog ': ' www.linuxzen.com ', ' name ': ' Cold Night '}
>>> info2
{' blog ': ' www.linuxzen.com ', ' name ': ' Cold '}

The code list above uses [:] to create a copy of a list instead of referring to a dictionary copy method you can also create a copy of a dictionary instead of a reference so that you can avoid the references previously mentioned

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.