I need to map one list onto another.l1 is a list of lists of lists and l2 is a list of numbers.I want elements of l1 to be the keys and elements of l2 as the values.I tried to convert l1 into a tuple and then mapping but it doesnt work.Pleeeez help!!

#below is what i want to do 
d = { tuple([tuple(['a': 1,'b': 2]),tuple(['a': 4,'b': 3])]),tuple(tuple(['a': 2,'c': 2]),tuple(['a': 1,'c': 3])]) : 0.4}
#a simpler example
d1 = {tuple(['a': 1,'b': 2]) : 0.4}

None of the above works :(

Recommended Answers

All 3 Replies

is an invalid construct and gives a syntax error!
Do you mean dictionary {'a': 1,'b': 2} ?

If you want the same data/info in a tuple you could use (('a', 1),('b', 2)) and later on ...

# convert tuple of tuples to dictionary
print dict((('a', 1),('b', 2)))   # {'a': 1, 'b': 2}

Sorry, this is little clumsy, but I am using good friend's notebook in busy coffee shop. I hope this explains what you want to do:

# list of lists
list1 = [['a', 1], ['b', 2]]

# list of numbers
list2 = [44, 77]

# convert list of lists to a tuple of tuples using list comprehension
tuple1 = tuple([tuple(sublist) for sublist in list1])

print tuple1  # (('a', 1), ('b', 2))

# create a dictionary from a tuple and a list using zip()
# the tuple forms the key and the list forms the value
dict1 = dict(zip(tuple1, list2))

print dict1  # {('a', 1): 44, ('b', 2): 77}

I'm sorry,i meant , instead of : in the code :(
I'll try the above method,thanks very much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.