How to get all data from set

So, it`s my data in list

[('AED', Decimal('3.67303')), ('AFN', Decimal('89.408409')), ('ALL', Decimal('118.735882')), ('AMD', Decimal('420.167855')), ('ANG', Decimal('1.803593')), ('AOA', Decimal('431.906'))]

And I did this but only for first element: Currency: AED, Value: 3.67303

 for key, val in query:
    return f'Currency: {key}, Value: {val}'

How can I do this for all?

>Solution :

If you want to return everything from a function create a new list with list comprehensions

def func():
    lst = [('AED', Decimal('3.67303')), ('AFN', Decimal('89.408409')), ('ALL', Decimal('118.735882')), ('AMD', Decimal('420.167855')), ('ANG', Decimal('1.803593')), ('AOA', Decimal('431.906'))]
    return [f'Currency: {key}, Value: {val}' for key, val in lst]

or use yield to create a generator

def func():
    lst = [('AED', Decimal('3.67303')), ('AFN', Decimal('89.408409')), ('ALL', Decimal('118.735882')), ('AMD', Decimal('420.167855')), ('ANG', Decimal('1.803593')), ('AOA', Decimal('431.906'))]
    for key, val in lst:
        yield f'Currency: {key}, Value: {val}'

another less explicit way to create a generator

def func():
    lst = [('AED', Decimal('3.67303')), ('AFN', Decimal('89.408409')), ('ALL', Decimal('118.735882')), ('AMD', Decimal('420.167855')), ('ANG', Decimal('1.803593')), ('AOA', Decimal('431.906'))]
    return (f'Currency: {key}, Value: {val}' for key, val in lst)

Output

for val in func():
    print(val)

Currency: AED, Value: 3.67303
Currency: AFN, Value: 89.408409
Currency: ALL, Value: 118.735882
Currency: AMD, Value: 420.167855
Currency: ANG, Value: 1.803593
Currency: AOA, Value: 431.906

Leave a Reply