Hello.
mylist = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2]
How can i delete that list indexes?
I tried:

    def clear(self, event):
        global numbers
        for i in numbers:
            print i
            del numbers[i]

But it didn't work:

   File "./kivycal", line 83, in clear
     del numbers[i]
 TypeError: list indices must be integers, not str

What can i do for that?

Recommended Answers

All 5 Replies

Just currect it:

numbers = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2]

To empty the list, use

numbers[:] = []

Yes!! Thank you @Gribouillis.

mylist = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2]
print(mylist)
# replace mylist with an empty list
mylist = []
print(mylist)   # --> []

mylist = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2]
# delete part of a list, keep the first 7 elements
del mylist[7:]
print(mylist)   # --> [6, '/', 3, '+', 9, '+', 8]

mylist = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2]
# delete part of a list, keep the last 3 elements
del mylist[:-3]
print(mylist)   # --> [1, '/', 2]

Thank you @vegaseat, was helpful too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.