0

I want to build a script getting 2 lists (same size) from the user, L1 and L2, and build a dictionary: each object from L1 is a key, and each object from L2 is the value (for same index)

I'm trying to make a combined list first, in order to use the dict() function.

here is my script. It runs, but prints nothing. I want to print just to make sure I get build the right list to apply the dict() function to...

    print 'enter 2 lists with same len:'

L1=input('enter first list:')
L2=input('enter second list:')

if len(L1)==len(L2) and isinstance(L1,list) and isinstance(L2, list):
    D_list=[]
    D_ij=[]
    for i in L1:
        if isinstance (i,(int, str)):
            for j in L2:
                if isinstance(j, (tuple, list)):

                    for i in L1:
                        for j in L2:



                            D_ij=[L1[i], L2[j]]
                            D_list.extend([D_ij])

                            print D_list

else: print 'error'
Amir Chazan
  • 173
  • 1
  • 7

2 Answers2

4

You need zip function:

https://docs.python.org/2/library/functions.html#zip

You can build dictionary like this:

my_dict = dict(zip(list_1, list_2))
Eugene Soldatov
  • 9,755
  • 2
  • 35
  • 43
1

You can do it with dictionary comprehension:

d = {k:v for k,v in zip(L1,L2)}
Iron Fist
  • 10,739
  • 2
  • 18
  • 34