Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Is there any reason to do anything more complicated than one of these two lines when you want to clear a list in Python?

old_list = [] 

old_list = list()

The reason I ask is that I just saw this in some running code:

del old_list[ 0:len(old_list) ]

1 Answer

0 votes
by (106k points)

By using del list_name[:] you can clear lists.

Example code is as follows:-

>>> a = [1, 2, 3] 

>>> b = a 

>>> del a[:] 

>>> print 

a, b [] []

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 14, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...