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,950 points
167 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, 2019 in Python by Nisa
• 1,090 points
163 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, 2019 in Python by Karthik
203 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
25,778 views
+1 vote
7 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
12,144 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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,461 views
0 votes
0 answers
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
162 views
0 votes
2 answers

How to use virtual environment in Python?

venv (for Python 3) and virtualenv (for Python 2) allow ...READ MORE

answered Aug 24 in Python by Pistle
• 300 points
82 views