Home > Mobile >  trying to use getter and setter in a list of dictionaries functions in python but I get the same err
trying to use getter and setter in a list of dictionaries functions in python but I get the same err

Time:11-03

I have a simple program that has to delete some values that are between 2 given "days". For example, I have this list of dicts:

lst=[{"day": 1, "sum": 25, "type": 'in'}, {"day": 2, "sum": 55, "type": 'in'}, {"day": 3, "sum": 154, "type": 'out'}, {"day": 4, "sum": 99, "type": 'in'}]

and I wanna delete the values with "day" values between 1 and 3 and the output should be:

[{"day": 4, "sum": 99, "type": 'in'}]

Now I am using this program:

def delete_transaction_interval(all_transactions, dayStart, dayEnd):
for element in enumerate(all_transactions):
    if get_transaction_day(all_transactions[element])>=dayStart and get_transaction_day(all_transactions[element])<=dayEnd:
        new_list_transactions=all_transactions[:]
return new_list_transactions

but I want to use a getter function instead of all_transactions[i]["day"]. I already created the function:

def get_transaction_day(all_transactions):
    return all_transactions["day"]

but I am using it I got this error:

list indices must be integers or slices, not tuple

and I don't know how to handle it because I do not see any tuple in my code TBH.

My version is:

def delete_transaction_interval(all_transactions, dayStart, dayEnd):
    i=0
    while i<=len(all_transactions)-1:
        if get_transaction_day(all_transactions[i])>=dayStart and get_transaction_day(all_transactions[i])<=dayEnd:
            new_transactions_list=all_transactions[:]
        else:
            i =1
    return new_transactions_list

Traceback:

    Exception has occurred: TypeError
list indices must be integers or slices, not tuple
  File "<String>", line 81, in delete_transaction_interval
    if get_transaction_day(all_transactions[element])>=dayStart and get_transaction_day(all_transactions[element])<=dayEnd:
  File "<String>", line 229, in test_delete_interval
    delete_transaction_interval(all_transactions,1,3)
  File "<String>", line 276, in test_all
    test_delete_interval()
  File "<String>", line 281, in <module>
    test_all()

Can somebody help me with this, please?

CodePudding user response:

Iterate over the list using a forloop if you want to get rid of the index

new_transactions_list = []
for elem in lst:
    if not 1 <= elem["day"] <= 3 :
        new_transactions_list.append(elem)
    
print(new_transactions_list)

Output

[{'day': 4, 'sum': 99, 'type': 'in'}]
    

CodePudding user response:

If you are desperate to use this function:

def get_transaction_day(all_transactions):
    return all_transactions["day"]

I have no problem with that. Small functions are good.

Its just that you must pass something in which works with ["day"]

Here are some examples:

def delete_transaction_interval(all_transactions, dayStart, dayEnd):
    for element in enumerate(all_transactions):
        if get_transaction_day(element[1])>=dayStart and get_transaction_day(element[1])<=dayEnd:
            new_list_transactions=all_transactions[:]
    return new_list_transactions

and from the other answer here:

def delete_transaction_interval(all_transactions, dayStart, dayEnd):
    new_transactions_list = []
    for elem in lst:
        if not dayStart <= get_transaction_day(elem) <= dayEnd:
        new_transactions_list.append(elem)
    return new_transactions

or a range() version:

def delete_transaction_interval(all_transactions, dayStart, dayEnd):
    for i in range(len(all_transactions)):
        if get_transaction_day(all_transactions[i])>=dayStart and get_transaction_day(all_transactions[i])<=dayEnd:
            new_transactions_list=all_transactions[:]
    return new_transactions_list

However, be warned, only one of these will work. The clue is to look in the original duplicate link which is here.

  • Related