-3

I have a list in this format:

a =[["1","2"], ["3","4"]]

I need to turn it into this format:

a =[("1","2"), ("3","4")]

thank you

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Riccardo moroletti
  • 281
  • 2
  • 3
  • 8
  • They are two different object types. [lists](https://docs.python.org/2/tutorial/datastructures.html#more-on-lists) are denoted by `[...]` and [tuples](https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences) are denoted by `(...)`. It is not that they have different brackets, they are entirely different data structures. For one thing, tuples are immutable while lists can be changed. – Farmer Joe Dec 04 '14 at 17:19

2 Answers2

3

use tuple and list comprehension :

>>> a=[tuple(i) for i in a]
>>> a
[('1', '2'), ('3', '4')]

Also you can use map function (less performance than list-comprehension):

>>> a=map(tuple,a)
>>> a
[('1', '2'), ('3', '4')]
Mazdak
  • 105,000
  • 18
  • 159
  • 188
2

Another solution, you can use map

>>> a =[["1","2"], ["3","4"]]
>>> map(tuple, a)
[('1', '2'), ('3', '4')]
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42