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

Is it possible to iterate over a list of lists and print out the name of the lists in addition to their contents?

I am trying to print out out the ticker symbol next to its two respective values within my nested list.

gme_infos = [45.95, 133.46]        # Number of shares, Cost basis
amc_infos = [49, 32.24]            # Number of shares, Cost basis
list_infos = [gme_infos,amc_infos]

for i in list_infos
     print(i)

My output so far is what you would expect

[45.95, 133.46]
[49, 32.24]

What can I do to have my printed values appear next to the list they stem from but as part of iterating?

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

ex

gme_infos [45.95, 133.46]
amc_infos [49, 32.24]

>Solution :

Make list_infos a dict, not a list.

list_infos = {'gme_infos': gme_infos, 'amc_infos': amc_infos}
for name, infos in list_infos.items():
    print(f"{name} {infos}")
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