search
HomeBackend DevelopmentPython TutorialSpecific analysis of list() lists in Python

List is the most flexible ordered collection object type in Python. Different from strings, lists can contain any kind of objects: numbers, strings, custom objects and even other lists. Column tables are mutable objects that support modification in place and can be modified by specifying offsets. Implementation of value transfer and sharding, list method calls, delete statements and other methods.

Commonly used methods in the list:

1.append(x) : Add an element to the end of the list.

>>> list=[1,2,3,4,5,6]
>>> print list
[1, 2, 3, 4, 5, 6]
>>> list.append(7)
>>> list.append(8)
>>> print list
[1, 2, 3, 4, 5, 6, 7, 8]
>>>

2.extend(L) : Expands the list by adding all elements of the specified list.

>>> list
[1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12]
>>> L=[100,200,300,400]
>>> list.extend(L)
>>> print list
[1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>>

3.insert(i,x) : Insert an element at the specified position. The first parameter is the index of the element to be inserted before it.

>>> print list
[1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>> list.insert(2,1000)
>>> print list
[1, 2, 1000, 3, 4, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>>

4.remove(x) : Delete the first element with value x in the linked list. If there is no such element, an error will be returned.

>>>> print list
[1, 2, 1000, 3, 4, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>> list.remove(1000)
>>> list
[1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>>

5.pop(i)          : Removes an element from the specified position in the linked list and returns it. If no index is specified, a.pop() returns the last element. The element is then removed from the linked list.

>>>> list
[1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>> list.pop(3)

4


>>> list
[1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300, 400]
>>> list.pop()
400
>>> list
[1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300]
>>>

6.index(x) : Returns the index of the first element in the linked list whose value is x.

>>>> list
[1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300]
>>> list.index(6)
4
>>>

7.count(x) : Returns the number of times x appears in the linked list.

>>>> list
[1, 2, 3, 3, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300]
>>> list.count(3)
3
>>> list.count(200)
1
>>>

8.sort() : Sort the elements in the linked list appropriately.

9.reverse() : Elements in the inverted linked list.

Use the list as a stack

The linked list method makes it easy to use the linked list as a stack, and the stack serves as specific data Structure, the first element entered is the last to be released (last in, first out). Use append() Method can

add an element to the top of the stack. Use pop() without specifying an index Method can release an element from the top of the stack.

Use the list as a queue

You can also use the linked list as a queue. The queue is a specific data structure. The element that enters first is released first (first in, first in). out). Use the append() method to add elements to the end of the queue, and call pop() with 0

as the parameter. Method can release the first entered element.

Delete elements from a list: Use del to delete elements in segments.

>>> list
[1, 2, 3, 3, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300]
>>> del list[2:4]
>>> list
[1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300]
>>> del list[0]
>>> list
[2, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300]
>>> del list[5:]
>>> list
[2, 3, 5, 6, 7]
>>> del list[0:]
>>> list
[]
>>> list.append(1)
>>> list.append(2)
>>> list.append(3)
>>> list
[1, 2, 3]
>>>

The above is the detailed content of Specific analysis of list() lists in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
php如何实现Redis的List操作php如何实现Redis的List操作May 26, 2023 am 11:51 AM

List操作//从list头部插入一个值。$ret=$redis->lPush('city','guangzhou');//从list尾部插入一个值。$ret=$redis->rPush('city','guangzhou');//获取列表指定区间中的元素。0表示列表第一个元素,-1表示最后一个元素,-2表示倒数第二个元素。$ret=$redis->l

如何使用 Vue 实现可折叠列表?如何使用 Vue 实现可折叠列表?Jun 25, 2023 am 08:45 AM

Vue是一款流行的JavaScript库,广泛应用于Web开发领域。在Vue中,我们可以很方便地实现各种组件和交互效果。其中,可折叠列表是一个比较实用的组件,它可以将列表数据分组,提高数据展示的可读性,同时又能够在需要展开具体内容时进行展开,方便用户查看详细信息。本文就将介绍如何使用Vue实现可折叠列表。准备工作在使用Vue实现可折叠列

java中JSONArray互相转换List怎么实现java中JSONArray互相转换List怎么实现May 04, 2023 pm 05:25 PM

1:JSONArray转ListJSONArray字符串转List//初始化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

如何使用C#中的List.Sort函数对列表进行排序如何使用C#中的List.Sort函数对列表进行排序Nov 17, 2023 am 10:58 AM

如何使用C#中的List.Sort函数对列表进行排序在C#编程语言中,我们经常需要对列表进行排序操作。而List类的Sort函数正是为此设计的一个强大工具。本文将介绍如何使用C#中的List.Sort函数对列表进行排序,并提供具体的代码示例,帮助读者更好地理解和应用该函数。List.Sort函数是List类的一个成员函数,用于对列表中的元素进行排序。该函数接

Java基础中List常用方法是什么Java基础中List常用方法是什么May 14, 2023 am 10:16 AM

一、List接口简介List是一个有序的集合、可重复的集合。它是继承Collection接口,在List集合中是可以出现重复的元素,可以通过索引(下标)来访问指定位置的元素。二、List常用方法——voidadd(intindex,Obejctelement)方法1.voidadd(intindex,Obejctelement)方法是把element元素插入在指定位置,后面的元素往后移一个元素。2.voidadd(intindex,Obejctelemen

Java中如何将数组转换为ListJava中如何将数组转换为ListApr 19, 2023 am 09:13 AM

一.最常见方式(未必最佳)通过Arrays.asList(strArray)方式,将数组转换List后,不能对List增删,只能查改,否则抛异常。关键代码:Listlist=Arrays.asList(strArray);privatevoidtestArrayCastToListError(){String[]strArray=newString[2];Listlist=Arrays.asList(strArray);//对转换后的list插入一条数据list.add("1"

为什么在Python中list.sort()不会返回已排序的列表?为什么在Python中list.sort()不会返回已排序的列表?Sep 18, 2023 am 09:29 AM

示例在这个例子中,我们先看看list.sort()的用法,然后再继续。在这里,我们创建了一个列表并使用sort()方法按升序排序-#CreatingaListmyList=["Jacob","Harry","Mark","Anthony"]#DisplayingtheListprint("List=",myList)#SorttheListsinAscendingOrdermyList.sort(

java中List中set方法和add方法的区别是什么java中List中set方法和add方法的区别是什么Apr 19, 2023 pm 07:49 PM

前言在Java中的常用的集合接口List中有两个非常相似的方法:Eset(intindex,Eelement);voidadd(intindex,Eelement);这两个方法都是在集合的指定位置插入指定的元素,那么这两个方法到底有什么区别呢?接下来我们通过ArrayList这个我们常用集合实现来看一下这两个方法的差异相同点首先我们来看一下这两个方法在ArrayList中的相同点他们都会在集合的指定位置插入新的元素,例如下面的例子:#在集合的第2位插入一个F#通过add方法插入Listlist=

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft