Delete all list entries of length unknown

flebber flebber.crue at gmail.com
Sun Oct 4 23:09:37 EDT 2009


Hi

Can someone clear up how I can remove all entries of a list when I am
unsure how many entries there will be. I have been using sandbox to
play essentially I am creating two lists a and b I then want to add a
to b and remove all b entries. This will loop and b will receive new
entries add it to a and delete again.

I am going wrong with slice and list deletion, I assign x = len(b) and
then attempting to delete based on this. Here is my sandbox session.
What part am I getting wrong?

#>>>
a = (1, 2, 3, 4)
b = (5, 6, 7, 8)
#>>>
a
#---
(1, 2, 3, 4)
#>>>
b
#---
(5, 6, 7, 8)
#>>>
a + b
#---
(1, 2, 3, 4, 5, 6, 7, 8)
#>>>
b
#---
(5, 6, 7, 8)
#>>>
len(b)
#---
4
#>>>
x = len(b)
#>>>
del b[0:x]
Traceback (most recent call last):
Error:   File "<Shell>", line 1, in <module>
Error: TypeError: 'tuple' object does not support item deletion
#>>>
b[0:x] = d
Traceback (most recent call last):
Error:   File "<Shell>", line 1, in <module>
Error: NameError: name 'd' is not defined
#>>>



More information about the Python-list mailing list