Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a dictionary into a list

Example:

something = {
    "1": {
        "2": {
            "3": {
                "4": {},
                "5": {},
                "7": {},
                },
            "8": {
                "9": {},
                "10": {}
            },
            "11": {
                "12": {
                    "13": {
                        "14": {
                            "15": {
                                "16": {
                                    "17": {
                                        "18": {}
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

I'm trying to convert this dictionary in to a list of items like this:

['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18']

What method should I use?

I already tried something.items(), but what I got back was:

[('1', {'2': {'11': {'12': {'13': {'14': {'15': {'16': {'17': {'18': {}}}}}}}}, '8': {'9': {}, '10': {}}, '3': {'5': {}, '4': {}, '7': {}}}})]

This is my first time posting here, so if I did anything wrong please let me know.

Thank you and sorry for the weird post.

like image 858
Neox Avatar asked Nov 20 '12 12:11

Neox


People also ask

How do I convert a dictionary to a list?

Python's dictionary class has three methods for this purpose. The methods items(), keys() and values() return view objects comprising of tuple of key-value pairs, keys only and values only respectively. The in-built list method converts these view objects in list objects.

How do I map a dictionary to a list?

Maps the values of a list to a dictionary using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value. Use map() to apply fn to each value of the list. Use zip() to pair original values to the values produced by fn .

How do you convert a dictionary to a list tuple?

Use the items() Function to Convert a Dictionary to a List of Tuples in Python. The items() function returns a view object with the dictionary's key-value pairs as tuples in a list. We can use it with the list() function to get the final result as a list.


1 Answers

You'll need to use a function to flatten your structure:

def flatten(d):
    for key, value in d.iteritems():
        yield key
        for sub in flatten(value):
            yield sub

(The .iteritems() should be replaced with .items() if you are using Python 3).

On python 3.3 and newer, you can also use the new yield from syntax:

def flatten(d):
    for key, value in d.items():
        yield key
        yield from flatten(value)

This will recursively yield all the keys. To turn that into a list use:

list(flatten(elements))

Since Python dictionaries are unordered, the ordering of the keys returned is not going to be sorted. You'll have to explicitly sort the result if you want your keys to have a specific ordering.

like image 168
Martijn Pieters Avatar answered Oct 18 '22 09:10

Martijn Pieters