Reputation: 27
(Python) I'm a beginner and need to change my list of lists into a tuple of tuples.
Input-> myList=[[0, 0], [0, 1], [2, 2], [1, 2]]
Output-> ((0,0),(0,1),(2,2),(1,2))
The first thing I'm trying to do is to split my list of lists, but I keep getting an Attribute Error.
myList = [[0, 0], [0, 1], [2, 2], [1, 2]]
myList = [item[0].split(",") for item in myList]
print(myList)
any help?
Upvotes: 1
Views: 109
Reputation: 3547
Using map
+ tuple
tuple(map(tuple, myList))
#((0, 0), (0, 1), (2, 2), (1, 2))
Upvotes: 2
Reputation: 1168
myList=[[0, 0], [0, 1], [2, 2], [1, 2]]
mytuple = tuple([tuple(elem) for elem in myList])
print(mytuple)
Upvotes: 1
Reputation: 544
Just convert your sublists into tuples.
tuple(tuple(item) for item in myList)
Upvotes: 3