Amyth
Amyth

Reputation: 32959

complicated list and dictionary lookup in python

I have a list of tuples and a dictionary of lists as follows.

# List of tuples
lot = [('Item 1', 43), ('Item 4', 82), ('Item 12', 33), ('Item 10', 21)]

# dict of lists
dol = {

    'item_category_one': ['Item 3', 'Item 4'],
    'item_category_two': ['Item 1'],
    'item_category_thr': ['Item 2', 'Item 21'],
}

Now I want to do a look-up where any item in any list within dol exists in any of the tuples given in lot. If this requirement is met, then i want to add another variable to that respective tuple.

Currently I am doing this as follows (which looks incredibly inefficient and ugly). I would want to know the most efficient and neat way of achieving this. what are the possibilities ?

PS: I am also looking to preserve the order of lot while doing this.

merged = [x[0] for x in lot]

for x in dol:
    for item in dol[x]:
        if item in merged:
            for x in lot:
                if x[0] == item:
                    lot[lot.index(x)] += (True, )

Upvotes: 4

Views: 116

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123880

First, build a set of all your values inside of the dol structure:

from itertools import chain
dol_values = set(chain.from_iterable(dol.itervalues()))

Now membership testing is efficient, and you can use a list comprehension:

[tup + (True,) if tup[0] in dol_values else tup for tup in lot]

Demo:

>>> from itertools import chain
>>> dol_values = set(chain.from_iterable(dol.itervalues()))
>>> dol_values
set(['Item 3', 'Item 2', 'Item 1', 'Item 21', 'Item 4'])
>>> [tup + (True,) if tup[0] in dol_values else tup for tup in lot]
[('Item 1', 43, True), ('Item 4', 82, True), ('Item 12', 33), ('Item 10', 21)]

Upvotes: 5

Related Questions