Get Values From A Tuple In A List In Python
x = Bookshop() x.orders = [ [1, ('5464', 4, 9.99), ('8274',18,12.99), ('9744', 9, 44.95)], [2, ('5464', 9, 9.99), ('9744', 9, 44.95)], [3, ('5464', 9, 9.99), ('88112', 11, 24.99)],
Solution 1:
To get those totals for a specific code you can do:
Code:
def get_value(orders_list, code):
return {order[0]: o[1] * o[2] for order in orders_list
for o in order[1:] if o[0] == code}
Test Data:
orders = [
[1, ("5464", 4, 9.99), ("8274", 18, 12.99), ("9744", 9, 44.95)],
[2, ("5464", 9, 9.99), ("9744", 9, 44.95)],
[3, ("5464", 9, 9.99), ("88112", 11, 24.99)],
[4, ("8732", 7, 11.99), ("7733", 11, 18.99), ("88112", 5, 39.95)]
]
print(get_value(orders, '5464'))
Results:
{1: 39.96, 2: 89.91, 3: 89.91}
Post a Comment for "Get Values From A Tuple In A List In Python"