1

I'm fairly new to Python, so I apologize if this question seems a little naive. I'm working on a final project for my comp sci class that involves visualizing some data, but I ran into some difficulty with it.

Basically, I'm using a CSV file (which you can find here) that contains reported emissions data from every country over a 29-year period -- but I only want to visualize data from the top five emitting countries in each given year. My code right now looks like this:

counter = 0
for i in range(30):

    global_emissions = {}

    for country in dataset: 
     '''dataset is a list of country-specific dictionaries; a key value for every year (e.g. '1991') corresponds to a float representing CO2 emissions in kt.''' 
        key = str(1990 + counter)
        emissions = country[key]

        global_emissions[country['COUNTRY']] = emissions

        sorted_emissions = {}

        for i in sorted(global_emissions.values()): # this loop is just here to match keys to sorted values
            for k in global_emissions.keys():
                if global_emissions[k] == i:
                    sorted_emissions[k] = global_emissions[k]

    counter += 1

The sorted_emissions list for any given year then comes out as a bunch of key-value pairs in ascending order of value. At the very end are the highest-polluting countries. In the year 1990, for example, it looks like this:

...'Germany': 955310.0, 'Japan': 1090530.0, 'Russian Federation': 2163530.0, 'China': 2173360.0, 'United States': 4844520.0}

For the dictionaries that get produced for all 29 years, I want to isolate these last five elements -- but since the keys of the most-polluting nations will change from year to year, I can't just access them by their key.

I tried indexing to them with numerical slicing (i.e. trying something like sorted_emissions[-1]), but this doesn't work with dictionaries. Is there any workaround for this, or maybe just a better way to do it? Any suggestions at all would be appreciated. Thanks so much!

Macintosh_89
  • 664
  • 8
  • 24
Nathan
  • 11
  • 1

1 Answers1

0

Because dictionaries are by default not sorted, i.e., the position of a key-value pair in a dictionary carries no meaning, you can't access any item through indexing. However, you can turn the dictionary into a list of tuples, where the keys are the first element, and the values are the second element of the tuple. Then you can write code to always access the second element in the tuple (i.e., at position [1]) and sort those values.

I hope I understood your question correctly and this helps. :)

ling_666
  • 19
  • 5
  • Thank you so much -- I can't believe I didn't think of that! Really appreciate your help. – Nathan Dec 03 '22 at 09:02
  • Good answer, just a small comment: Python dictionaries are [insertion ordered since Python 3.6](https://stackoverflow.com/a/39980744/9152905). But it's still true that you can't access them by position ;-) – ffrosch Dec 05 '22 at 08:46