Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make ordered dictionary from list of lists?

The problem is: Having a list of names, and a list of lists, how to create a list, in which each item is an ordered dictionary with names as keys, and items from list of lists as values? It might be more clear from code below:

from collections import OrderedDict

list_of_lists = [
                ['20010103', '0.9507', '0.9569', '0.9262', '0.9271'],
                ['20010104', '0.9271', '0.9515', '0.9269', '0.9507'],
                ['20010105', '0.9507', '0.9591', '0.9464', '0.9575'],
                ]

names = ['date', 'open', 'high', 'low', 'close']

I would like to get:

ordered_dictionary = [
                     OrderedDict([('date', '20010103'), ('open', '0.9507'), ('high', '0.9569'), ('low', '0.9262'), ('close', '0.9271')]),
                     OrderedDict([('date', '20010104'), ('open', '0.9271'), ('high', '0.9515'), ('low', '0.9269'), ('close', '0.9507')]),
                     OrderedDict([('date', '20010105'), ('open', '0.9507'), ('high', '0.9591'), ('low', '0.9464'), ('close', '0.9575')]),
                     ]
like image 809
atman Avatar asked Mar 13 '13 10:03

atman


People also ask

Can you turn a list into a dictionary?

To convert a list to a dictionary using the same values, you can use the dict. fromkeys() method. To convert two lists into one dictionary, you can use the Python zip() function. The dictionary comprehension lets you create a new dictionary based on the values of a list.

How do I make an ordered dictionary?

OrderedDict is part of python collections module. We can create an empty OrderedDict and add items to it. If we create an OrderedDict by passing a dict argument, then the ordering may be lost because dict doesn't maintain the insertion order. If an item is overwritten in the OrderedDict, it's position is maintained.

How do I convert a nested list to a dictionary?

We can convert a nested list to a dictionary by using dictionary comprehension. It will iterate through the list. It will take the item at index 0 as key and index 1 as value.


2 Answers

Use zip() to combine the names and the values. With a list comprehension:

from collections import OrderedDict

ordered_dictionary = [OrderedDict(zip(names, subl)) for subl in list_of_lists]

which gives:

>>> from pprint import pprint
>>> pprint([OrderedDict(zip(names, subl)) for subl in list_of_lists])
[OrderedDict([('date', '20010103'), ('open', '0.9507'), ('high', '0.9569'), ('low', '0.9262'), ('close', '0.9271')]),
 OrderedDict([('date', '20010104'), ('open', '0.9271'), ('high', '0.9515'), ('low', '0.9269'), ('close', '0.9507')]),
 OrderedDict([('date', '20010105'), ('open', '0.9507'), ('high', '0.9591'), ('low', '0.9464'), ('close', '0.9575')])]
like image 104
Martijn Pieters Avatar answered Oct 06 '22 00:10

Martijn Pieters


I know this question is very old, but I thought I'd suggest a namedtuple solution as an alternative to OrderedDict that would work well in this situation:

from collections import namedtuple

Bar = namedtuple('Bar', ['date', 'open', 'high', 'low', 'close'])

bars = [Bar(date, o, h, l, c) for date, o, h, l, c in list_of_lists]

>>> bars
[Bar(date='20010103', open='0.9507', high='0.9569', low='0.9262', close='0.9271'),
 Bar(date='20010104', open='0.9271', high='0.9515', low='0.9269', close='0.9507'),
 Bar(date='20010105', open='0.9507', high='0.9591', low='0.9464', close='0.9575')]

>>> bars[2].date
'20010105'

>>> bars[2].close
'0.9575'

Even better, one could use dictionary comprehension with the date as the key:

Bar = namedtuple('Bar', ['open', 'high', 'low', 'close'])

bars = {date: Bar(o, h, l, c) for date, o, h, l, c in list_of_lists}

>>> bars
{'20010103': Bar(open='0.9507', high='0.9569', low='0.9262', close='0.9271'),
 '20010104': Bar(open='0.9271', high='0.9515', low='0.9269', close='0.9507'),
 '20010105': Bar(open='0.9507', high='0.9591', low='0.9464', close='0.9575')}

>>> bars['20010105']
Bar(open='0.9507', high='0.9591', low='0.9464', close='0.9575')

>>> bars['20010105'].close
'0.9575'
like image 29
Alexander Avatar answered Oct 06 '22 00:10

Alexander