Some of the things to be aware of in the use of Python dictionaries and lists

Source: Internet
Author: User
Python has three very useful data structures, lists, tuples and dictionaries, tuples are immutable, the list can hold any type of Python object, and can arbitrarily extend no size limit, the dictionary is a key-value type of key-value mapping, can hold any Python object, Dictionaries can be nested, values can be dictionary tuples or dictionaries

This is a Python dictionary and a list of traps rather than some of the features of Python, if you do not understand these features will cause some difficult to find the bug

Let's introduce these features.

All of the use of lists and dictionaries in Python is simply 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   # assigned 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 '

We see that if the list or dictionary is re-assigned to another variable and does not achieve the desired effect, we change one while the other is also changing at the same time, if we want to keep a snapshot, it is obvious that we do not achieve the effect we want, there is a common use, because we know the ordinary variable passed to function, the internal change of the function does not affect the external variables, then the list and the dictionary? Let's take a look at the following code, we create a function, a dictionary adds a key and a value, and a 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 above code is obviously not the result we want, if this list/dictionary is only used in one place it may not happen if we need the same list to be processed elsewhere, if you do not know this feature will produce a very difficult to find bug when the above is not what we want, how can we avoid it? We can make a copy of the list/dictionary instead of 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 a copy method that references a dictionary you can also create a duplicate of a dictionary instead of a reference so that you can avoid the case of a previously mentioned reference

  • Related Article

    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.