Fabiana Rossi
Fabiana Rossi

Reputation: 27

List of lists into tuple of tuples

(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

Answers (4)

Transhuman
Transhuman

Reputation: 3547

Using map + tuple

tuple(map(tuple, myList))
#((0, 0), (0, 1), (2, 2), (1, 2))

Upvotes: 2

Sowjanya R Bhat
Sowjanya R Bhat

Reputation: 1168

myList=[[0, 0], [0, 1], [2, 2], [1, 2]]

mytuple = tuple([tuple(elem) for elem in myList])

print(mytuple)

Upvotes: 1

ilyankou
ilyankou

Reputation: 1329

tuple([ tuple(x) for x in myList ])

Upvotes: 1

Paweł Kowalski
Paweł Kowalski

Reputation: 544

Just convert your sublists into tuples.

tuple(tuple(item) for item in myList)

Upvotes: 3

Related Questions