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 6040439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:30:51+00:00 2026-05-23T06:30:51+00:00

I have an dictonary for my input with the following characteristics: Each value will

  • 0

I have an dictonary for my input with the following characteristics:

  • Each value will be either an integer, string or iterable (other than a string).
  • If the element is an iterable, each element in that iterable will only be a string or integer.

ex:

mydict = {
    'one': 1,
    'two': '23',
    'three': 3,
    'four': [
        7,
        '6',
        5,
        8
    ],
    'nine': 9
}

I need to convert the input to a list of tuples where each tuple is a key/value pair. For iterable elements, there will be a key/value pair for each of its elements, sorted by value. For example, output for the above should be:

('four', 5)
('four', 7)
('four', 8)
('four', '6')
('nine', 9)
('one', 1)
('three', 3)
('two', '2')

I currently have this implemented using the following generator:

def dict_to_sorted_tuples(unsorted_dict):
    for key in sorted(unsorted_dict):
        if isinstance(unsorted_dict[key], basestring):
            yield key, unsorted_dict[key]
            continue
        try:
            for v in sorted(unsorted_dict[key]):
                yield key, v
        except:
            yield key, unsorted_dict[key]

print list(dict_to_sorted_tuples(mydict))

I feel this can be done in a cleaner fashion, any suggestions for improvements?

  • 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-23T06:30:52+00:00Added an answer on May 23, 2026 at 6:30 am
    >>> sorted((i,k) for i,j in mydict.items() for k in ([j] if isinstance(j, str) or isinstance(j, int) else j))
    [('four', 5), ('four', 7), ('four', 8), ('four', '6'), ('nine', 9), ('one', 1), ('three', 3), ('two', '2')]
    

    The idea here is that if the value is an int or a str, you put it in a list. Now the problem is simplified because you have a value you can always iterate over

    If you are really sure you only need to check for int or str (not subclasses or unicode), you could just use

    sorted((i,k) for i,j in mydict.items() for k in ([j] if type(j) in (int, str) else j))
    

    If the value can be unicode, you should use isinstance(j, basestring) instead of isinstance(j, str)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following json returned string: [{status:2,id:-1,content:User has entered wrong input,time:1346765646202}] (as you
I have a dictionary, user_dict, and create a list for each user value, this
I have the following string that I would like to Huffman-encode and store efficiently
The following will make more sense if you have ever played minecraft. Since many
I have a table that contains URL strings, i.e. /A/B/C /C/E /C/B/A/R Each string
I have a program which gets commands as a string. Each character in the
I have dictionary, like Dictionary<string, bool> accValues = new Dictionary<string, bool>() And I want
Does VBA have dictionary structure? Like key<>value array?
I have a Dictionary where the key and value are both strings. It's possible
I have the following two functions which take two dictionaries and recursively add their

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.