Python Learning In Debian Linux-List of tuples and dictionaries

Source: Internet
Author: User

List,TuplesAndDictionaryIt is a collection of data elements that are organized together in a certain way and belong to the data structure.

ListAndTuplesThe main difference is that the list can be modified, and the tuples cannot be modified. It is more suitable to use tuples when the sequence cannot be modified.

List and metadata areReference stored values by number

DictionaryReference stored values by name

List of first lectures

1. List

1.1 List Initialization
Empty list: list_none = []

Initialize an empty list with a fixed length: list_none1 = [none] * 5 # An empty list with a length of 5. None is a built-in value of Python, indicating that nothing exists.

Instance:

Non-empty list: listtest = [1, 2, 3, 'A', 'B', 'C']

Listtest1 = ['Hi'] * 5

Instance:

1.2 list operations

Len: Returns the number of elements in the list.

Max: Returns the largest element in the list.

Min: Returns the smallest element in the list.

Instance:

ListFunction: converts all types of sequences to a list.

You can assign values to an element based on the index. The index must exist.

You can assign values to elements in a certain range (partition assignment). You can replace them with fragments of a sequence that is not long enough as the original sequence.

DEL: deletes an element.

Append: append a new element.

Count: count the number of times an element appears in the list.

Extend: append multiple values in another sequence at the end of the list.

Index: finds the index of the first matching item of a value from the list.

Insert: insert an object to the list.

Pop: removes an element from the list (the last one is removed by default). This method returns a value.

Remove: removes the first match for a value in the list.

Reverse: stores elements in the list in reverse order.

Sort: sorts the list in the original position and returns a null value.

Note: SortThe method has two optional parameters: Key and reverse. The key parameter is a function, and the reverse parameter is a simple boolean value of true or false.

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.