Home Backend Development Python Tutorial Detailed explanation of Python list (List) operation methods

Detailed explanation of Python list (List) operation methods

Jan 18, 2017 pm 03:54 PM

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

1. Create a list
Just enclose the different data items separated by commas in square brackets. As shown below:

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];
Copy after login
is the same as the index of the string, and the list index starts from 0. Lists can be intercepted, combined, etc.
2. Access the values ​​in the list

Use the subscript index to access the values ​​in the list. You can also use square brackets to intercept characters, as shown below:

#!/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]
Copy after login

The output result of the above example:

list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]
Copy after login

3. Update the list
You can modify or update the data items of the list, or you can use the append() method to add list items, as shown below:

#!/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];
Copy after login

The output result of the above example:

Value available at index 2 :
1997
New value available at index 2 :
2001
Copy after login

4. Delete the elements of the list
You can use the del statement to delete the elements of the list, as shown in the following example:

#!/usr/bin/python
list1 = ['physics', 'chemistry', 1997, 2000];
print list1;
del list1[2];
print "After deleting value at index 2 : "
print list1;
Copy after login

The output result of the above example :

['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]
Copy after login

5. Python list script operators
The list operators + and * are similar to string operators. The + sign is used for combined lists, and the * sign is used for repeated lists.


is as follows:

Python 列表(List)操作方法详解

6. Python list interception
Python list interception and string operations Type, as follows:

L = ['spam', 'Spam', 'SPAM!']
Copy after login

Operation:

Python 列表(List)操作方法详解

7. Functions and methods of Python list operations
List operations include the following functions:
1. cmp(list1, list2): Compare the elements of two lists
2. len(list): The number of list elements
3. max(list): Return the maximum value of the list elements
4. min(list): Returns the minimum value of list elements
5. list(seq): Convert tuples to lists
List operations include the following methods:
1. list.append(obj): At the end of the list Add a new object
2, list.count(obj): Count the number of times an element appears in the list
3, list.extend(seq): Append multiple elements from another sequence at the end of the list at one time value (extend the original list with a new list)
4. list.index(obj): Find the index position of the first matching item of a value from the list
5. list.insert(index, obj): Insert the object into the list
6, list.pop(obj=list[-1]): Remove an element in the list (default is the last element), and return the value of the element
7, list.remove(obj): Remove the first matching item of a value in the list
8, list.reverse(): Reverse the elements in the list
9, list.sort([func]): Sort the original list

For more detailed explanations of Python list (List) operation methods and related articles, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How Do I Use Beautiful Soup to Parse HTML? How Do I Use Beautiful Soup to Parse HTML? Mar 10, 2025 pm 06:54 PM

How Do I Use Beautiful Soup to Parse HTML?

Image Filtering in Python Image Filtering in Python Mar 03, 2025 am 09:44 AM

Image Filtering in Python

How to Download Files in Python How to Download Files in Python Mar 01, 2025 am 10:03 AM

How to Download Files in Python

How to Use Python to Find the Zipf Distribution of a Text File How to Use Python to Find the Zipf Distribution of a Text File Mar 05, 2025 am 09:58 AM

How to Use Python to Find the Zipf Distribution of a Text File

Intro to Flask: Adding a Contact Page Intro to Flask: Adding a Contact Page Feb 28, 2025 am 10:03 AM

Intro to Flask: Adding a Contact Page

How to Work With PDF Documents Using Python How to Work With PDF Documents Using Python Mar 02, 2025 am 09:54 AM

How to Work With PDF Documents Using Python

How to Cache Using Redis in Django Applications How to Cache Using Redis in Django Applications Mar 02, 2025 am 10:10 AM

How to Cache Using Redis in Django Applications

How to Perform Deep Learning with TensorFlow or PyTorch? How to Perform Deep Learning with TensorFlow or PyTorch? Mar 10, 2025 pm 06:52 PM

How to Perform Deep Learning with TensorFlow or PyTorch?

See all articles