Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7560501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:55:11+00:00 2026-05-30T12:55:11+00:00

I have a dictionary that I’ve converted to a list so I can sort

  • 0

I have a dictionary that I’ve converted to a list so I can sort by the first item. The key in the dictionary is a string (of numbers), the value is an integer which is maintained in the list.
The list from the dictionary conversion looks like:

[('228055', 1), ('228054', 1), ('228057', 2), ('228056', 1), ('228051', 1), ('228050', 1),     ('228053', 1), ('203184', 6), ('228059', 1), ('228058', 1), ('89370', 2), ('89371', 3), ('89372', 2), ('89373', 1), ('89374', 1), ('89375', 1), ('89376', 1), ('89377', 1), ('89378', 1), ('89379', 1),.........]

There are around 240,000 items in the dictionary. I would like to sort the dictionary by the first index, but when I use itemgetter(0) it sorts the list by all the “1’s” first. The sorted listed looks like:

[('0', 3), ('1', 3), ('10', 3), ('100', 4), ('1000', 3), ('10000', 1), ('100000', 3), ('100001', 2), ('100002', 3), ('100003', 3), ('100004', 2), ('100005', 2), ('100006', 2), ('100007', 2), ('100008', 2), ('100009', 2), ('10001', 1), ('100010', 3), ('100011', 3), ('100012', 3), ('100013', 2), ('100014', 1), ('100015', 1), ('100016', 1), ('100017', 1), ('100018', 1), ....]

I would like the list to be sorted by [‘0’, 3), (‘1’, 3), (‘2’, integer), (‘3’, integer),…(‘240,000’, integer)]

Here’s my code where I’m reading in a text file to a dictionary, converting to a list and used itemgetter to sort by first item in nested list. I need the dictionary in the code because I heavily depend on it to look up values by the key. I’m only trying to sort the dictionary for the output file once all the processes are ran. Thanks for any help.

import sys, string, csv, arcpy, os, fileinput, traceback
from arcpy import env
from operator import itemgetter


#Creating a dictionary of FID: LU_Codes from external txt file
text_file = open("H:\SWAT\NC\FID_Whole_Copy.txt", "rb")
#Lines = text_file.readlines()
FID_GC_dict =  dict()
reader = csv.reader(text_file, delimiter='\t')
for line in reader:
    FID_GC_dict[line[0]] = int(line[1])
text_file.close()

dict_List = [(x, FID_GC_dict[x]) for x in FID_GC_dict.keys()]
dict_List.sort(key=itemgetter(0))
print dict_List
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:55:13+00:00Added an answer on May 30, 2026 at 12:55 pm

    Changing the key to convert the string to an int will help you, also here are some other sorting tips.

    from operator import itemgetter
    
    list_to_sort=[('89372', 2), ('89373', 1), ('89374', 1), ('89375', 1), ('89376', 1),     ('89377', 1), ('228055', 1), ('228054', 1), ('228057', 2), ('228056', 1), ('228051', 1), ('228050', 1),('228053', 1), ('203184', 6), ('228059', 1), ('228058', 1), ('89370', 2), ('89371', 3), ('89372', 2), ('89373', 1), ('89374', 1), ('89375', 1), ('89376', 1), ('89377', 1)]
    print list_to_sort
    
    list_to_sort.sort()
    print list_to_sort # badly sorted as described
    
    list_to_sort.sort(key=itemgetter(0))
    print list_to_sort # badly sorted as described (same as above)
    
    list_to_sort.sort(key=lambda x: int(x[0]))
    print list_to_sort # sorted well
    
    list_to_sort.sort(key=lambda x: int(x[0]), reverse=True)
    print list_to_sort # sorted well in reverse
    

    Side note on building the list to sort from the dict. iteritems() is a nicer way of doing what you do with the following

    dict_List = [(x, FID_GC_dict[x]) for x in FID_GC_dict.keys()]
    
    dict_List = [(k,v) for k,v in FID_GC_dict.iteritems()]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Dictionary that is declared thusly: Dictionary myDictionary<string, List<FCPort>> = new Dictionary<string,
I have a dictionary that I normally access with a key, so I need
I have a Dictionary object that is formed using a double as its key.
I have a Dictionary<string,int> that has the potential to contain upwards of 10+ million
I have dictionary entries that are read to a Dictionary <string,string> myDict=null; The entries
Currently, I have a dictionary that has a number as the key and a
Assume we have dictionary that translates strings into numbers. How to reverse it into
I have a dictionary that hold address details, sometimes it can hold, 'name, email,
I want to have a dictionary that assigns a value to a set of
I have a python dictionary object that contains a boolean for every key, e.g.:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.