Ad

Is It Possible To Iterate Over A List Of Lists And Print Out The Name Of The Lists In Addition To Their Contents?

- 1 answer

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]
Ad

Answer

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}")
Ad
source: stackoverflow.com
Ad