Common methods of python list
Commonly used methods in Python list include creating a list, adding new elements, viewing values in the list, deleting elements in the list, sorting and reversing, and list slicing.
Lists are the most commonly used Python Data type, which can appear as a comma-separated value within square brackets. In the following article, we will introduce the common methods of lists in Python in detail. It has a certain reference effect and I hope it will be helpful to everyone.
[Recommended course: Python Tutorial]
Common methods of Python list
(1) Create a list
Use commas to separate different data before use Just enclose it in square brackets. The subscript starts from 0. The subscript of the last element can be written as -1
list = ['1',‘2,‘3’] list = [] //空列表
(2) Add a new element
There are three ways to add new elements, namely:
append method: add an element at the end of the list
list.append()
insert method: means to add an element at the specified position, if not specified Add
list.insert(n,'4')
extend method at the end of the list: merge elements in list 1 and list 2
list1.extend(list2)
(3) View the values in the list
You can use the print method to traverse the list. This method is equivalent to for i in list
print(list[n]): means using the subscript index to access the value in the list
print(list.count(xx)): Indicates checking the number of an element in this list. If the element does not exist, it returns 0
print(list.index(xx)): Indicates Find the subscript of this element. If there are multiple ones, return the first one. If you find an element that does not exist, an error will be reported
(4) Delete the element in the list
list.pop(): Delete the last element
list.pop(n): Specify the subscript and delete the specified element. If you delete a non-existent element, an error will be reported
list .remove(xx): Delete an element in the list. If there are multiple identical elements, delete the first one
del list[n]: Delete the element corresponding to the specified subscript
del list: Delete the entire list. After the list is deleted, it cannot be accessed
(5) Sorting and reversing
list.reverse(): Indicates reversing the list
list.sort(): Indicates sorting, ascending order by default
list.sort(reverse=True): Indicates descending order
Note: There are strings and numbers in the list cannot be sorted, the sorting is for the same type
(6) list slicing
Slicing is a method of list value
name[n: m]: Indicates that the slice does not contain the value of the subsequent element
name[:m]: Indicates that if the previous value of the slice is default,
name[n:] will be taken from the beginning. : Indicates that if the value after the slice is defaulted, it will be taken to the end
name[:] : It means that if all values are defaulted, all
name[n:m:s] will be taken: Indicates how many elements to take once, where s represents the step size
If the step length is a positive number, it is taken from left to right
If the step length is a negative number, it is taken from right to left
Note: Slicing also applies to strings, and strings also have subscripts
Summary: The above is the entire content of this article, I hope it will be helpful to everyone.
The above is the detailed content of Common methods of python list. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



List operation //Insert a value from the head of the list. $ret=$redis->lPush('city','guangzhou');//Insert a value from the end of the list. $ret=$redis->rPush('city','guangzhou');//Get the elements in the specified range of the list. 0 represents the first element of the list, -1 represents the last element, and -2 represents the penultimate element. $ret=$redis->l

1: JSONArray to ListJSONArray string to List//Initialize JSONArrayJSONArrayarray=newJSONArray();array.add(0,"a");array.add(1,"b");array.add(2,"c") ;Listlist=JSONObject.parseArray(array.toJSONString(),String.class);System.out.println(list.to

Method to convert list to numpy: 1. Use the numpy.array() function. The first parameter of the function is a list object, which can be a one-dimensional or multi-dimensional list; 2. Use the numpy.asarray() function, which will try its best to Use the data type of the input list; 3. Use the numpy.reshape() function to convert the one-dimensional list into a multi-dimensional NumPy array; 4. Use the numpy.fromiter() function, the first parameter of the function is an iterable object.

Example In this example, we first look at the usage of list.sort() before continuing. Here, we have created a list and sorted it in ascending order using sort() method - #CreatingaListmyList=["Jacob","Harry","Mark","Anthony"]#DisplayingtheListprint("List=",myList)#SorttheListsinAscendingOrdermyList .sort(

How to sort a list using the List.Sort function in C# In the C# programming language, we often need to sort the list. The Sort function of the List class is a powerful tool designed for this purpose. This article will introduce how to use the List.Sort function in C# to sort a list, and provide specific code examples to help readers better understand and apply this function. The List.Sort function is a member function of the List class, used to sort elements in the list. This function receives

1. The most common way (not necessarily the best) is through Arrays.asList(strArray). After converting the array into List, you cannot add or delete the List, you can only check and modify it, otherwise an exception will be thrown. Key code: Listlist=Arrays.asList(strArray);privatevoidtestArrayCastToListError(){String[]strArray=newString[2];Listlist=Arrays.asList(strArray);//Insert a piece of data into the converted list list.add(" 1"

1. Introduction to List interface List is an ordered collection and a repeatable collection. It inherits the Collection interface. Repeated elements can appear in the List collection, and the element at the specified position can be accessed through the index (subscript). 2. List common methods - voidadd (intindex, Obejctelement) method 1. The voidadd (intindex, Obejctelement) method inserts the element element at the specified position and moves the subsequent element back one element. 2.voidadd(intindex,Obejctelemen

1. Overview of the Map Collection Framework The Map collection framework is a key-value pair data structure that allows you to use keys to find and store values. Each key in the Map is unique and can only be associated with one value. Common implementations in the Map collection framework include HashMap, TreeMap and LinkedHashMap. 1.HashMapHashMap is the most widely used Map implementation in Java. It stores data based on hash tables. HashMap has excellent performance, and the time complexity of search and insertion operations is O(1), but it does not guarantee the order of elements. Demo code: Mapmap=newHashMap
