-5

I wanna delete the values of the list

lists = [12,15,15,15,3,15,1,6,4,7,888,56248]
while len(lists) is True:
    lists.pop()
print(lists)

But i got this output :

[12, 15, 15, 15, 3, 15, 1, 6, 4, 7, 888, 56248]
vaultah
  • 44,105
  • 12
  • 114
  • 143
Young Mind
  • 11
  • 2
  • 7
    `len(lists)` is an integer. An integer is never the same object as the Boolean singleton `True`. Perhaps you meant just `while lists`? – kindall Dec 06 '17 at 18:17
  • 1
    @Matthias agreed and hammered open. The duplicate is talking about iterating over a list while mutating it. This is simply misunderstanding a conditional. – Adam Smith Dec 06 '17 at 18:23
  • 2
    You could use `while lists: lists.pop()`. The better solution would be `lists = []`. If it has to be the same list (with the same id) use `lists[:] = []`. – Matthias Dec 06 '17 at 18:24
  • 4
    @Matthias `lists.clear()` – Stefan Pochmann Dec 06 '17 at 18:25
  • yes your are right i should put it in bool() – Young Mind Dec 06 '17 at 18:27
  • 1
    Fair enough. The faulty conditional is the main problem. And my dupe targets don't apply because he's popping from the end of the list, which is safe to do. Sorry about that. – PM 2Ring Dec 06 '17 at 18:31
  • @StefanPochmann: You're so right. I had a `Coffee not found error`. – Matthias Dec 06 '17 at 18:39

2 Answers2

2

The problem here is in your conditional

while len(lists) is True:

is checks for identity, not equality.

[1, 2, 3] == [1, 2, 3]  # True
[1, 2, 3] is [1, 2, 3]  # False, they are two distinct (but equivalent) lists.

However even equality would be incorrect here, since

42 == True           # False
2 == True            # False
any_nonzero == True  # False
# notably 1 == True
# and     0 == False
# but still (1 is True) == False!

You can coerce an integer into a boolean

bool(42) == True           # True
bool(2) == True            # True
bool(any_nonzero) == True  # True

But it's usually better to just leave the coercion to Python

while lists:
    lists.pop()
# or more simply:
# lists = []
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • yes you are right i forgot . i should put it in bool() .thank you – Young Mind Dec 06 '17 at 18:30
  • Because `0` means `False` in python, and anything else is true. This means that the code will keep deleting the values until the statement is false (when there are no values in the list). – AJ123 Dec 06 '17 at 18:54
  • @StefanPochmann either will work, but you're right that `while lists` is better. I'll edit – Adam Smith Dec 06 '17 at 19:10
  • @AJ123 not anything else. The empty string (`""`) and relevantly empty collections (`[]`, `{}`, `set()`, etc) are also Falsey. `while lists` is definitely the idiomatic approach. – Adam Smith Dec 06 '17 at 19:11
-1

If by "delete" you mean removing the element and reducing the list, you can iterate through a copy of lists with a for loop:

for n in lists[:]:
    lists.remove(n)

If, instead, you want to have a list of None (or 0) values as long as lists, you can do it like this:

newlists = [0] * len(lists)

As was suggested in the previous comment.