The difference between list, tuple, and dict in Python

Source: Internet
Author: User

Dictionary
  1. . Dictionary is one of the built-in data types in Python, and he defines the relationship between the key and the value.
  2. Each element has a key-value pair, and the entire element set is enclosed in curly braces {}.
  3. You can get value from key, but you cannot get the key through value. The reason is: Dictionary inside key is unique, but value is not unique.
  4. In a dictionary, you cannot have two identical keys at the same time, assigning a value to an existing key overrides the original value, and you can add key-value pairs at any time.
  5. When used in dictionary, key is sensitive to case. Key and key are considered to be different keys.
  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 dictionary, the data type of value can be different and can be mixed and matched as needed. Dictionary's key requires much more rigor, but he can also be a string, integer, and other type. 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 COM you and a dictionary inside remove all the elements. The empty curly brace collection represents the dictionary of an element.
  9. List
    1. A list is a collection 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 list[0]
    3. The index of a negative number starts counting forward from the end of the list to access the element. Any of the last elements of a non-empty list is list[-1]
    4. The list can be split using List.slice () and List[1:3], and a list can be copied directly using the list ":"
    5. Append can add elements to the end of the list
    6. Insert to move a single element to the fixed position of the list
    7. 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.
    8. 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
    9. 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.
    10. Index finds the first occurrence of a value in the list and returns the index value.
    11. To test whether a value is inside the list, use in, and return TrueIf the value exists, otherwise return to False .
    12. Remove removes the first occurrence of a value from the list.
    13. 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.
    14. Lists can also be connected with the + operator. List = list + otherlist 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.
    15. 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.
    16. 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.
Touple

  

    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 difference between list, tuple, and dict in Python

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.