Python list (list) operation method detailed _python

Source: Internet
Author: User
Tags data structures python list in python

Lists are the most basic data structures in Python, and lists are the most commonly used Python data types, and the data items of a list do not need to have the same type. Each element in the list is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.
Python has a built-in type of 6 sequences, but the most common is lists and tuples. Sequences can be performed by including indexing, slicing, adding, multiplying, and checking the members. In addition, Python has built-in methods for determining the length of a sequence and determining the largest and smallest elements.

First, create a list
Just enclose the different data items separated by commas in square brackets. As shown below:

Copy Code code as follows:
List1 = [' Physics ', ' Chemistry ', 1997, 2000];
List2 = [1, 2, 3, 4, 5];
LIST3 = ["A", "B", "C", "D"];

As with the index of the string, the list index starts at 0. The list can be intercepted, combined, and so on.
second, the value in the Access list
Use the subscript index to access the values in the list, and you can also intercept the characters using square brackets, as follows:
Copy Code code as follows:
#!/usr/bin/python

List1 = [' Physics ', ' Chemistry ', 1997, 2000];
List2 = [1, 2, 3, 4, 5, 6, 7];

Print "list1[0]:", list1[0]
Print "List2[1:5]:", List2[1:5]


The above example outputs the result:
Copy Code code as follows:
List1[0]: Physics
List2[1:5]: [2, 3, 4, 5]

third, update the list
You can modify or update the data items in the list, or you can use the Append () method to add the list items, as follows:
Copy Code code as follows:
#!/usr/bin/python

List = [' Physics ', ' Chemistry ', 1997, 2000];

Print "Value available at index 2:"
Print list[2];
LIST[2] = 2001;
Print "New value available at index 2:"
Print list[2];

The above example outputs the result:

Copy Code code as follows:
Value available at index 2:
1997
New value available at index 2:
2001

iv. Delete list elements
You can use the DEL statement to delete the elements of a list, as follows:
Copy Code code as follows:
#!/usr/bin/python

List1 = [' Physics ', ' Chemistry ', 1997, 2000];

Print List1;
Del list1[2];
print "After deleting value at index 2:"
Print List1;


The above example outputs the result:
Copy Code code as follows:
[' Physics ', ' Chemistry ', 1997, 2000]
After deleting value at index 2:
[' Physics ', ' chemistry ', 2000]

Five, Python list script operator
The list of + and * operators are similar to strings. The + number is used to combine the list, and the * is used to repeat the list.

As shown below:

Python Expression Results Description
Len ([1, 2, 3]) 3 Length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Combination
[' hi! '] * 4 [' hi! ', ' hi! ', ' hi! ', ' hi! '] Repeat
3 in [1, 2, 3] True Whether an element exists in the list
For x in [1, 2, 3]: print x, 1 2 3 Iterations

Six, Python list interception
Python's list intercepts and string manipulation types, as follows:
Copy Code code as follows:
L = [' spam ', ' spam ', ' spam! ']

Operation:
Python Expression Results Description
L[2] ' spam! ' Read the third element in the list
L[-2] ' Spam ' Read the penultimate element in the list
L[1:] [' Spam ', ' spam! '] To intercept a list starting with the second element

functions and methods of Python list operations
The list operation contains the following functions:
1, CMP (List1, LIST2): Comparing elements of two lists
2, Len (list): List element number
3, Max (list): Returns the maximum value of the listing element
4, Min (list): Returns the minimum value of the element
5. List (seq): Convert a tuple to a list
list operations include the following methods:
1, List.append (obj): Add a new object at the end of the list
2, List.count (obj): Statistics the number of times an element appears in the list
3, List.extend (seq): At the end of the list, append multiple values from another sequence (expand the original list with the new list)
4, List.index (obj): Find the index position of the first occurrence of a value from the list
5, List.insert (index, obj): inserting objects into a list
6, List.pop (Obj=list[-1]): Remove an element from the list (the default last element), and return the value of the element
7, List.remove (obj): Remove the first occurrence of a value in the list
8, List.reverse (): Reverse the elements in the list
9, List.sort ([func]): Sort the original list

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.