Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to empty a Python list without doing list = []?

Tags:

python

types

If the my_list variable is global, you can't do:

my_list = []

that just create a new reference in the local scope.

Also, I found disgusting using the global keyword, so how can I empty a list using its methods?

like image 787
Juanjo Conti Avatar asked Apr 08 '26 07:04

Juanjo Conti


1 Answers

del a[:]

or

a[:] = []
like image 102
Daniel Stutzbach Avatar answered Apr 10 '26 00:04

Daniel Stutzbach