Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement list Operation in Python

Shulou Source: shulou.com Published: 2022-06-02 08:21:28 09月30日 Update

This article shows you how to achieve list operation in Python, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Delete elements ("delete" del, pop, remove) analogy in real life, if a classmate changes classes, then the name of the student after this note should be deleted; this feature is often used in development.

Common ways to delete list elements are:

Del: delete based on subscript

Del is a Python statement, not a list method, and cannot be called through list. Using del, you can delete an element, and when the element is deleted, the element behind it automatically moves to fill the vacant position.

Pop: deletes a specified location element

Using pop (), you can also get the element at the specified location in the list, but after the fetch is complete, the element is automatically deleted. If an offset is specified for pop (off), it returns the element where the offset corresponds. If not specified,-1 is used by default. So pop (0) returns the header element, while pop () or pop (- 1) returns the end element of the list.

Remove: delete based on the value of the element

Demo: (del)

MovieName = ['Pirates of the Caribbean', 'Empire of hackers', 'first drop of Blood', 'Lord of the Rings', 'Hobbits', 'Fast and Furious'] print ('- before deletion -') for tempName in movieName: print (tempName) del movieName [2] print ('- after deletion -') for tempName in movieName: print (tempName)

Results:

-before deletion-the first drop of blood in the Caribbean pirate hacker empire, the speed and passion of the hobbits-after deletion-the speed and passion of the hobbits of the Caribbean pirate hacker empire

Demo: (pop)

MovieName = ['Pirates of the Caribbean', 'Empire of hackers', 'first drop of Blood', 'Lord of the Rings', 'Hobbits', 'Fast and Furious'] print ('- before deletion -') for tempName in movieName: print (tempName) movieName.pop () print ('- after deletion -') for tempName in movieName: print (tempName)

Results:

-before deletion-the first drop of Blood Lord of the Rings in the Pirates of the Caribbean Hacker Empire, Speed and Passion of the Hobbits-after deletion-the first drop of Blood King of the Hobbits in the Pirates of the Caribbean Empire

Demo: (remove)

MovieName = ['Pirates of the Caribbean', 'Empire of the Hacker', 'first drop of Blood', 'Lord of the Rings', 'Hobbits', 'Fast and Furious'] print ('- before deletion -') for tempName in movieName: print (tempName) movieName.remove ('Lord of the Rings') print ('- after deletion -') for tempName in movieName: print (tempName)

Results:

-before deletion-the first drop of blood in the Caribbean Pirates Hacker Empire, the speed and passion of the Hobbits-after the deletion-the first drop of blood in the Caribbean Pirates Hacker Empire-the speed and passion of the Hobbits the above is how to achieve list operation in Python, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Elements Rings Lord of the Rings Pirate Galle Caribbean Empire Hacker Passion Speed location result content skill method knowledge offset conciseness brevity subscript Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi NVidia Apple macOS Shulou Information