[Go] Python list, tuple, dict difference

Source: Internet
Author: User
Tags python list

From:http://www.cnblogs.com/michael-kong/archive/2012/07/11/2585840.html

    1. Dictionary is one of the built-in data types for Python, which defines the relationship between the key and the value.
    2. Each element is a Key-value pair, and the entire element set is enclosed in curly braces.
    3. You can reference its value by key, but you cannot get key by value
    4. There can be no duplicate key in a dictionary.  Assigning a value to an existing key overrides the original value. New key-value can be added at any time. This syntax is the same as modifying the existing value.
    5. When using dictionary, you need to know: Dictionary key is case-sensitive
    6. Dictionary is not just for storing strings. The value of Dictionary can be any data type, including strings, integers, objects, and even other Dictionary.  In a single dictionary, the value of dictionary does not need to be all the same data type and can be mixed and matched as needed. Dictionary's keys are much stricter, but they can be strings, integers, and several other types (which are discussed later). You can also mix and match key data types in a dictionary
    7. del allows you to remove individual elements from a dictionary using key.
    8. Clear clears all elements from a dictionary. Note the empty Brace collection represents a dictionary with no elements.

  1. The list is a set of ordered elements enclosed in square brackets.
  2. The List can be used as an array starting with the 0 subscript. The first element of any non-empty list is always li[0]
  3. a negative index accesses an element by counting forward from the tail of the list. The last element of any non-empty list is always li[-1]. If the negative index makes you feel confused, you can understand this: li[-n] = = Li[len (li)-n]. So in this list, li[-3] = = li[5-3] = = li[2].
  4. you can get a subset of the list by specifying 2 indexes, called a "slice". The return value is a new list that contains all the elements in the list that start in order from the first slice index (here , li[1]), until but not including the second slice index (here, li[3]).
  5. if all two shard indexes are omitted, this will include all elements of the list. But unlike the original list named Li, it is a new list that has exactly the same elements as Li . li[:] is a shorthand for generating a list full copy.
  6. Append appends a single element to the end of the list.
  7. Insert Inserts a single element into the list. The numeric parameter is the index of the insertion point. Note that the elements in the list do not have to be unique, there are two independent elements that have the same value as ' new ' .
  8. extend is used to connect to list. Note that you do not use multiple parameters to invoke extend, which is called using a list parameter.
  9. Lists's two methods extend and append look similar, but in fact they are completely different. extend accepts a parameter, which is always a list, and adds each element in the list to the original list
  10. On the other hand, append accepts a parameter, which can be any data type, and is simply appended to the tail of the list. Call the append method here using a list parameter containing 3 elements.
  11. Index finds the first occurrence of a value in the list and returns the index value.
  12. To test whether a value is inside the list, use in, and return TrueIf the value exists, otherwise return to False .
  13. Remove removes the first occurrence of a value from the list.
  14. Pop is an interesting thing. It does two things: delete the last element of the list, and then return the value of the deleted element. Note that this differs from Li[-1] , which returns a value but does not change the list itself. Also differs from li.remove (value), which changes the list but does not return a value.
  15. Lists can also be connected with the + operator. List = list + otherlist is equivalent to list. Extend (otherlist). But the + operator returns a new (connected) list as a value, and extend only modifies the existing list. In other words, for large lists, the extend executes faster.
  16. Python supports the + = operator. Li + = [' both '] is equivalent to li.extend ([' both ']). The + = operator can be used for lists, strings, and integers, and it can also be overloaded for user-defined classes.
  17. The * operator can act as a repeating device on the list. Li = [1, 2] * 3 equals to li = [1, 2] + [1, 2] + [1, 2], and three lists are connected as one.
    1. A tuple is an immutable list. One is that creating a tuple cannot change it in any way.
    2. A tuple is defined in the same way as a list, except that the entire set of elements is surrounded by parentheses rather than square brackets.
    3. The elements of a tuple are sorted in the same order as the list. The tuples index is the same as list starting from 0, so the first element of a non-empty tuple is always t[0].
    4. A negative index is counted as a list from the tail of a tuple.
    5. As with list shards (slice) can also be used. Note that when a list is split, a new list is obtained, and when a tuple is split, a new tuple is obtained.
    6. Tuple has no method: No append or extend method, no remove or pop method, no index method, can use in to see if an element exists in a tuple.

The list () function and the tuple () function accept objects that can be stroked today (such as a sequence) as parameters, and create a new list and tuple by shallow copying the data. Although strings are also sequence types, they are generally not used for list () and tuple (). more cases, They are used for Xuan exchange between two types, such as you need to convert an existing tuple into a list type (then you can modify its elements), or vice versa.

alist=[' 123 ', ' 456 '];

Atuple=tuple (alist);

Print Atuple

>>> (' 123 ', ' 456 ')

Alist==atuple

>>> False

Alist2=list (Atuple)

Alist2==alist

>>>true

Alist is Alist2

>>>false

Again ID () confirm, [ID (x) for x in Alist,atuple,alist2]

>>>[10903800,12003900,11730280]

So either list () or tuple () is not possible to complete the conversion, that is, passing a tuple to the list does not become a real list, while passing a list to a tuple () will not become a true tuple. Although the previous two objects have the same data set, But the variables point to not the same object. Note that all of their values are the same, and a list cannot be "equal" to a tuple.

[Go] Python list, tuple, dict difference

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.