Empty a list in python.

0 votes

I have a list and I have to clear the list. I am using this :

while len(alist) > 0 : alist.pop()

Is there a better way?

Dec 17, 2018 in Python by Anjali
• 2,930 points
50 views

1 answer to this question.

0 votes

You could use the clear() method of list which is parallel to clear() of dict, set, deque and other mutable in Python 3.3 container types:

alist.clear()  # removes all items from alist (equivalent to del alist[:])

The same can also be achieved with alist *= 0.

answered Dec 17, 2018 by Shuvodip

Related Questions In Python

0 votes
1 answer

How to check if a list is empty in python?

To check if a list is empty ...READ MORE

answered May 27 in Python by Nisa
• 1,090 points
39 views
0 votes
1 answer

How do I check if a list is empty in python?

Hey @Vedant, that's pretty simple and straightforward: if ...READ MORE

answered May 27 in Python by Karthik
39 views
+4 votes
7 answers

Splitting a List into chunks in Python

Here's a generator that yields the chunks ...READ MORE

answered Apr 13, 2018 in Python by Nietzsche's daemon
• 4,260 points
4,904 views
+1 vote
4 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
592 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 220 views
0 votes
1 answer

What does calling a function means in Python?

Calling a function means that you are ...READ MORE

answered Dec 17, 2018 in Python by Shuvodip
41 views
0 votes
1 answer

How to use virtual environment in Python?

Virtual environment is very common with Python ...READ MORE

answered Dec 17, 2018 in Python by Shuvodip
33 views