Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete list item automatically in python?

Tags:

python

If i have a list for example : num=[1,2,3,4] and i want to delete list[0] every 2 second, so after 2 second list in num is num = [2,3,4] and 2 second after it will be num =[2,3] how can i do that?

like image 623
Tiffany Avatar asked Dec 27 '18 15:12

Tiffany


People also ask

How do you delete a item from a list Python?

The remove() method removes the first matching element (which is passed as an argument) from the list. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.

How do I remove items from pandas list?

In Python, use list methods clear() , pop() , and remove() to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.

What is the difference between remove () and Del for list in Python?

The remove() function removes the first matching value from the list. The pop() function is used to return the removed element from the list. The del() function is used to delete an element at a specified index number in the list.


1 Answers

You could do it using time.sleep, and del to remove the first element:

for i in range(len(num)):
    time.sleep(2)
    del num[0]
like image 65
yatu Avatar answered Oct 22 '22 03:10

yatu