How to get all data from set

Viewed 31

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?

4 Answers

try:

from decimal import Decimal
a = [('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 i in a:
    print(f'Currency: {i[0]}, Value: {i[1]}')

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

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

Your return statement breaks the loop, so just the first element will get returned.

It looks like you want to print it in the console?!

Then just change the line to: print('Currency: {key}, Value: {val}')

When you want to do something else with it, please be more accurate, what you want to do.

You could return a list of format strings then print them by unpacking that list. Like this:

from decimal import Decimal

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

def data(q):
    return [f'Currency={c}, value={v}' for c, v in q]

print(*data(query), sep='\n')

Output:

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
Related