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

Python list how to delete items

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to delete items from the python list. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Use the remove () method of the list object. You need to specify the items to delete.

If it occurs more than once, the first such item is deleted.

> myList = ["prem", 1jre 2jin3, "sai", 2pyrr1] > myList.remove (2) > my list ['prem', 1,3,' sai', 2JE3] > myList.remove (4) backtracking (last call last time): file ", line 1, in ValueError: list.remove (x): X is not in list2

2. Use the pop method of list object. This method takes the index of the item as a parameter and pops up the item at that index.

IndexError is thrown if the index is out of range.

> myList.pop (1) > my list ['prem', 3,' sai', 2, 3, 1] > myList.pop (7) backtracking (last called last): file ", line 1, in index error: pop-up index out of range

3. Use the operator del.

This operator gets the index of the item to be deleted and deletes the item at that index.

The operator also supports deleting a series of items in the list. If I know the items in the list, this is my preferred way to delete items. This is a clear and quick way to delete a project. This operator also throws an IndexError when the index / index is out of range.

> del myList [2] > my list ['prem', 3, 2, 3, 1] > del myList [1:3] > > my list [' prem', 3,1] > > del myList [7] backtracking (last call): file ", line 1, in IndexError: list assignment index out of range thank you for reading! This is the end of the article on "how to delete items from the python list". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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: 0

*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

Development

Wechat

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

12
Report