pingwin850
pingwin850

Reputation: 297

Join two lists into one dictionary

These are my lists (both lists have the same length - 43 indices):

list1 = [u'UMTS', u'UMTS', u'UMTS', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'GSM', u'LTE']

list2 = [u'60000', u'60000', u'60000', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'120000', u'120000', u'120000', u'120512', u'120512', u'120512', u'120512', u'120512', u'120512', u'118629', u'118629', u'118629', u'120000', u'120000', u'120000', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'300000']

I would like to join them into one dictionary:

dictionary = {
    'UMTS' : 'indices from 0 to 2'
    'GSM' : 'indices from 3 to 42'
    'LTE' : 'index 43'
}

Does anyone know how to do it? Is it possible at all? Thanks in advance !!!

Upvotes: 0

Views: 64

Answers (3)

Lokesh Sanapalli
Lokesh Sanapalli

Reputation: 1034

Tried with a single for loop. Isn't this what you want?

>>> for i in range(0,len(list1)):
...     if(mydict.get(list1[i]) is None):
...             mydict[list1[i]]=[list2[i]]
...     else:
...             mydict[list1[i]].append(list2[i])
...
>>> mydict
{u'UMTS': [u'60000', u'60000', u'60000'], u'GSM': [u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'120000', u'120000', u'120000', u'120512', u'120512', u'120512', u'120512', u'120512', u'120512', u'118629', u'118629', u'118629', u'120000', u'120000', u'120000', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629'], u'LTE': [u'300000']}

Upvotes: 1

Rahul K P
Rahul K P

Reputation: 16081

You can implement like this.

result = {}
for i in set(list1):
     result.update({i:[]})
for i,j in zip(list1,list2):
     result[i].append(j)

Result

{u'UMTS': [u'60000', u'60000', u'60000'], u'GSM': [u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'120000', u'120000', u'120000', u'120512', u'120512', u'120512', u'120512', u'120512', u'120512', u'118629', u'118629', u'118629', u'120000', u'120000', u'120000', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629'], u'LTE': [u'300000']}

Concept

Created a dictionary with the possible values in the list1 and an empty list as value. With using set you will get the elements only once. And iterate through the list1 and list2 and insert values directly to the list corresponding.

Upvotes: 1

Kasravnd
Kasravnd

Reputation: 107347

You can use collections.defaultdict() and zip() function:

>>> from collections import defaultdict
>>> 
>>> d = defaultdict(list)
>>> 
>>> for i, j in zip(list1, list2):
...     d[i].append(j)
... 
>>> d
defaultdict(<type 'list'>, {u'UMTS': [u'60000', u'60000', u'60000'], u'GSM': [u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'120000', u'120000', u'120000', u'120512', u'120512', u'120512', u'120512', u'120512', u'120512', u'118629', u'118629', u'118629', u'120000', u'120000', u'120000', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629', u'118629'], u'LTE': [u'300000']})

Upvotes: 2

Related Questions