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

Trouble printing out list elements from dictionary

I am a student trying to learn the basics of programming in Python. For the sake of practice, I am working on a function that reads data from a file, put the two columns in question in two lists, and then assigns the two lists as a key-value pair in a dictionary.

I think I manage that task fairly OK (although it is probably better and cleaner ways of doing this).

However, my problem is that I for some reason don’t manage to print out the contents for the two lists the way I want.

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

The data file I am reading from is:

2016 Ukrania
2017 Portugal
2018 Israel
2019 Nederland
2020 Avlyst
2021 Italia

My code looks like:

def ReadEuroVisionInfo(file):

    with open(file) as infile:
        year = []
        country = []

        for line in infile:
            w = line.strip().split(" ")
            year.append(int(w[0]))
            country.append(w[1])

        return {"Year": year, "Country": country}

euroVision = ReadEuroVisionInfo("songcontest.txt")


# This is where the problem is
for i in euroVision:
    for j in i:
        print(euroVision[i], euroVision[j])

I want the output in the terminal to be like this:

2016 Ukrania
2017 Portugal
2018 Israel
2019 Nederland
2020 Avlyst
2021 Italia

The error message I’m getting is:

    print(euroVision[i], euroVision[j])
KeyError: 'Y'

Is there anyone who can help me fix this?

>Solution :

You have a dict with two keys, each value is a list. You can do like this

for year, country in zip(d["Year"], d["Country"]):
    print(year, country)

FOLLOW UP

To answer the OP comment:

  • you could organize your data structure differently: every year is going to have one and only one winner (is ex-aequo possible), so you could return a dataframe like this instead:
def ReadEuroVisionInfo(file)
    with open(file) as infile:
        return dict(line.strip().split(" ") for line in infile)

then you could print it simply doing:

for year, country in ReadEuroVisionInfo("songcontest.txt").items():
    print(year, country)
  • if you want to keep the structure you have at the moment, you could alternatively do like this – even if I would not recommend it:
for i in range(len(d["Year"]):
    print(d["Year"][i], d["Country"][i])

I think it should be save given the way you read the file in – d["Year"] and d["Country"] will always have the same length, so you won’t get any IndexError in this specific case.

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