Home > OS >  Is it possible to iterate over a list of lists and print out the name of the lists in addition to th
Is it possible to iterate over a list of lists and print out the name of the lists in addition to th

Time:02-21

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?

ex

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

CodePudding user response:

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}")
  • Related