How can I change an item that is a tuple inside a dict

Viewed 44

I am trying to make a function that converts the currency in a given dict:

q = {
    '1': ('tissue', '200', '30USD'), 
    '2': ('shampoo', '300', '75000RUB'), 
    '3': ('vaseline', '180', '10USD'), 
    '5': ('cup', '0', '15USD'), 
    '4': ('chips', '150', '100USD'), 
    '6': ('chocolate', '0', '20USD')
}

This is the function i came up with:

def convert_price(q, rate=24000):
    prices = []
    converted_dict={}
    
    for value in q.values():
        l_value=list(value)
        target = l_value[2]
        new_price = target
        if target[-3:] == "RUB":
            new_price = str(int(target[:-3])/rate) + "USD"
            l_value[2] = new_price
            prices.append(new_price)
        else:
            prices.append(target)
    #print(prices)
    
    for price in prices:
        for id in q.keys():
            new = str(price)
            if new in q[id]:
                converted_dict[id]=q[id]
                break
    print(converted_dict)

convert_price(q)

The function above adds the new prices to the "prices" list. But it can't add the changed price because it couldn't find the same value witrh the same currency in the dict. How should I change the code to make it work?

2 Answers

Is this what you need? In your current code converted_dict doesn't contain key "2" because its price is in Rubel. If you need to add it. Below code will work.

def convert_price(q, rate=24000):
    prices = []
    converted_dict={}
    
    for value in q.values():
        l_value=list(value)
        target = l_value[2]
        new_price = target
        if target[-3:] == "RUB":
            new_price = str(int(target[:-3])/rate) + "USD"
            l_value[2] = new_price
            prices.append(new_price)
        else:
            prices.append(target)
    # print(prices)
    

    for i in range(len(prices)):
        id = str(i+1)
        temp_list= list(q[id])
        temp_list[2] = prices[i]
        converted_dict[id] = tuple(temp_list)
    print(converted_dict)

convert_price(q)

I'd suggest you to change your item list data type and implement a dynamic change_rate dict:

def convert_price(q, convert_to):
    for items in q:
        currency = items['currency']
        if currency != convert_to:
            rate = change_rate[f"{currency}/{convert_to}"]
            items['price'] = str(float(items['price']) * rate)
            items['currency'] = convert_to
    return q

if __name__ == '__main__':
    q = [
        {'id': '1', 'item': 'tissue', 'amount': '200', 'price': '30', 'currency': 'USD'}, 
        {'id': '2', 'item': 'shampoo', 'amount': '300', 'price': '75000', 'currency': 'RUB'}, 
        {'id': '3', 'item': 'vaseline', 'amount': '180', 'price': '10', 'currency': 'USD'},
        {'id': '5', 'item': 'cup', 'amount': '0', 'price': '15', 'currency': 'USD'},
        {'id': '4', 'item': 'chips', 'amount': '150', 'price': '100', 'currency':'USD'}, 
        {'id': '6', 'item': 'chocolate', 'amount': '0', 'price': '20', 'currency':'USD'}
    ]

    change_rate = {
        'RUB/USD': 1/24000
    }

    converted_items = convert_price(q, convert_to='USD')
    print(converted_items)
Related