开发者

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

开发者 https://www.devze.com 2023-01-13 17:30 出处:网络
If the my_list variable is global, you can\'t do: my_list = [] that just create a new reference in the local scope.

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开发者_JS百科 methods?


del a[:]

or

a[:] = []


How about the following to delete all list items:

def emptyit():
    del l[:]
0

精彩评论

暂无评论...
验证码 换一张
取 消