When i tried the following in python interpreter,i got a syntax error:

d = {[[['a': 1,'b': 2],['a': 4,'b': 3]][['a': 2,'c': 2],['a': 1,'c': 3]]] : 0.4}

Pleeeez help :sad:

11 Years
Discussion Span
Last Post by bumsfeld

Dictionary keys have to be immutable objects. Lists are mutable objects. Comvert all the lists to tuples.


how can i convert a list into a tuple...i know the difference is in the brackets(curly for tuples and square for lists) but how to actually do it for a list of lists?
Basically i have 2 lists of equal length and i want to create a mapping from l1 to l2. So i have to use dictionary.Any ideas on how to do this?
EDIT:sorry for this...i should have googled a bit :)


Here is a possible way to create tuples from lists:

list1 = [1, 2, 3]
tuple1 = tuple(list1)

print list1   # [1, 2, 3]
print tuple1  # (1, 2, 3)

list2 = [[1, 2], [3, 4]]
tuple2 = tuple([tuple(sublist) for sublist in list2])

print list2   # [[1, 2], [3, 4]]
print tuple2  # ((1, 2), (3, 4))
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.