zip four lists to create dictionary with keys from two lists and values from another two lists

Question:

I have four lists:

list_a = ['hello', 'yes', 'you']
list_a_ind = [0, 1, 2]
list_b = ['he', 'here', 'great'] 
list_b_ind = [1, 2, 3]

I want to create a dictionary with combined index as keys and combined elements as values, result as below:

dictionary_ab = {(0,1): ('hello', 'he'), 
                 (1,2): ('yes', 'here'), 
                 (2,3): ('you', 'great)}

what’s the fastest way to do it?

Asked By: Ling

||

Answers:

You can use dict() directly with zip():

out = dict(zip(zip(list_a_ind, list_b_ind), zip(list_a, list_b)))
print(out)

Prints:

{(0, 1): ("hello", "he"), 
 (1, 2): ("yes", "here"), 
 (2, 3): ("you", "great")}
Answered By: Andrej Kesely

This snippet will do the trick:

dict(zip(zip(list_a_ind, list_b_ind), zip(list_a, list_b)))
Answered By: Masked Man

The straightforward approach is a single zip unpacked and repacked via a dictcomp:

{(a, b): (c, d) for a, b, c, d in zip(list_a_ind, list_b_ind, list_a, list_b)}

For your short example inputs, this is the fastest solution (it has the lowest setup overhead), but the nested zip solution eventually wins out for large enough inputs (it has lower per-item overhead). This is a little easier to read to my mind, being a little more descriptive of what you’re doing and how, but not enough to strongly prefer it (it’s really fine either way).

Answered By: ShadowRanger
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.