Rusu Alex
Rusu Alex

Reputation: 23

List of Tuples remove error

ExpenseL is my list of tuples and I try to remove from the list starting from start to stop but I just get this error: in removeFromAtoB expenseL.pop(i) TypeError: 'tuple' object cannot be interpreted as an integer Please help me!!! :)

def removeFromAtoB():
    aux = copy.deepcopy(expenseL)
    print(expenseL, "\n")
    start = int(input("Starting point: "))
    stop = int(input("Ending point: "))
    j = 0
    for i in expenseL:
        if j >= start and j <= stop:
            expenseL.pop(i)
        j += 1
    print(expenseL)

Upvotes: 1

Views: 110

Answers (1)

L3viathan
L3viathan

Reputation: 27323

You're iterating over your list of tuples:

for i in expenseL

That means i will be one of those tuples. Then you try to use it in list.pop:

expenseL.pop(i)

This won't work, because list.pop expects an index. Just enumerate your list:

for index, tpl in enumerate(expenseL):
    ...
    expenseL.pop(index)

But this breaks, too, because the indices change when you remove an element. You could circumvent that by not increasing j in that case, but the simpler way is just assigning an empty list to the slice:

def removeFromAtoB():
    start = int(input("Starting point: "))
    stop = int(input("Ending point: "))
    expenseL[start:stop+1] = []

Upvotes: 2

Related Questions