List Usage Big Summary

Source: Internet
Author: User

The list is a collection in Python, and the elements in the collection can be any data type, with the following usage:

Note: The following list of elements are in the case of a string, if the number or Boolean value is not added '

1> Building a list

Set name = [' Element 1 ', ' element 2 ', ' element 3 ', ...]

Example classmates = [' Hi ', ' hallo ', ' yellow ']

2> View a list

Enter the name of the collection directly

Example classmates

3> the length of a list, which has several elements

Len (collection name)

Example Len (classmates)

4> viewing elements in a list at a specific location

collection name [number]

Example Classmates[0] Note: The numbers here are the same as the C language starting with the 0 record

Python also supports reverse view

collection name [-number]

Example Classmates[-1]

5> adds new elements to the list at the end

Collection name. Append (' new element ')

Example Classmates.append (' Hola ')

6> inserting a new element at a specified position

Collection name. Insert (position, ' new element ')

Example Classmates.insert (1. ' Wow ')

7> Delete last Element

Collection name. Pop ()

Example Classmates.pop ()

8> delete the specified position element

Collection name. Pop (location)

Example Classmates.pop (1)

9> replacing the specified position element

collection name [position] = ' new element '

Example classmates[1] = ' haha '

10> contains another list in a list

Set name 1 = [' element 1 ', ' element 2 ', [' element 1 ', ' Element 2 '], ' element 3 ']

Example s = [' lala ', ' Heihei ', [' Xixi ', ' AA '], ' Lulu ']

11> to view an element of another list in a list

Collection name 1[location 1][position 2] Note: Position 1 must be the location of the included list, position 2 is also starting from 0 record

Example S[2][0]

List Usage Big Summary

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.