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 18, 2018 in Python by Anjali
• 2,950 points
706 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 18, 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
1,049 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 28, 2019 in Python by Karthik
1,069 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
35,403 views
+1 vote
8 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
35,999 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer

What does calling a function means in Python?

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

answered Dec 18, 2018 in Python by Shuvodip
1,051 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 25, 2020 in Python by Pistle
• 300 points
819 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP