Combine two lists of lists into a dictionary python
Question: I'm not experienced in programming and I have a problem with combining two
lists of parsed sentences (=list within list) into a dictionary. I'm using
python 2.6.6

I have two lists of sentences, one in English and the other one in German. The
sentences correspond: the first sentence in the German list is a translation
of the first sentence in the English list etc. My goal is to be able to access
the English sentence, extract the subject and see whether the German subject
corresponds to the English one.

My two lists look like this (simplified):

    
    
    sentences_en = [[u'sid(s1).', u'sentence1', ...], [u'sid(s2).', u'sentence2', ...]]
    sentences_de = [['sid(s1).', 'Satz1', ...], ['sid(s2).', 'Satz2', ...]]
    

Each item in the list contains the sentence ID ('sid(s1).'), the actual
sentence ('sentenceX' or 'SatzX'), as well as more information on the sentence
('...'). Both lists ('sentences_en', 'sentences_de') contain 10000 items.

I'd like to map the English to the German sentences in a parallel dictionary
that looks like this:

    
    
    parallel_dict = {[u'sid(s1).', u'sentence1', ...]:['sid(s1).', 'Satz1', ...], [u'sid(s2).', 'sentence2', ...]:['sid(s2).', 'Satz2', ....]}
    

As I know that I can't have a list as the key, I tried to turn one of the
lists into a tuple and use this as the key (I found this solution on Stack
Overflow). Unfortunately, this doesn't seem to work:

    
    
    parallel_dict = {}
    tuple_sentences_en = tuple(sentences_en)
    parallel_dict = zip(tuple_sentences_en, sentences_de)
    

When I print parallel_dict, I get the following structure:

    
    
    [([u'sid(s1).', u'sentence1', ...], ['sid(s1).', 'Satz1', ...]), ([u'sid(s2).', u'sentence2', ...], ['sid(s2).', 'Satz2', ...])]
    

It does map the English sentence 1 to the German sentence 1 etc, but it is
most certainly not a dictionary - rather a list of tuples.

Does anyone know whether it is even possible to turn this structure into a
dictionary with the English sentences as the keys and the German sentences as
the values? Or is there a better way to work with parallel data?

I'd really appreciate your help!


Answer: Since this is for 2.6, use a generator expression for the `dict()`
constructor:

    
    
    from itertools import izip
    
    dict((en[0], (en[0], de[0])) for en, de in izip(sentences_en, sentences_de))
    

This uses `izip()` to avoid creating an intermediary list of 10000 items.

The loop unpacks each pair into two lists, from the english and german lists
before forming the key-value pair for the output dictionary.

If you can upgrade to Python 2.7 or 3.x, you can use a dict comprehension:

    
    
    {en[0]: (en[0], de[0]) for en, de in izip(sentences_en, sentences_de)}
    

