Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading