Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement list Operation in Python

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 283

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report