Convert Tuple to List and Back

How to convert a tuple of tuples to a list of lists?

Is this what you want? -

In [17]: a = ((1,2,3),(4,5,6),(7,8,9))

In [18]: b = [list(x) for x in a]

In [19]: b
Out[19]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

This is called list comprehension, and when you do list(x) where x is an iterable (which also includes tuples) it converts it into a list of same elements.

Converting tuple to list

this is what you are looking for

mytuple = (('7578',), ('6052',), ('8976',), ('9946',))
result = [int(x) for x, in mytuple]
print(result)

Convert tuple of list to list

Here it looks like you want the element inside the tuple, so if you do this:

myList = tupleA[0]

You'll get the list inside the tuple

Convert a tuple of tuples to list and remove extra bracket

use for loop to match data:

sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))

target = 'orange,apple,coconut'

found = False

for item in sql_data:
if target in item:
found = True

print(found)

Output:

True

Use list compression to find weather target is found or not:

sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))

target = 'orange,apple,coconut'

out = [True if target in item else False for item in sql_data]

print(True in out)

Output:

True

Use lambda to find weather target value exits or not:

from functools import reduce
sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))

target = 'orange,apple,coconut'

found = reduce(lambda bool_value1, bool_value2:bool_value1 or bool_value2,[True if target in item else False for item in sql_data])

print(found)

Output:

True

How do I convert tuple of tuples to list in one line (pythonic)?

This works as well:

>>> tu = (('aa',), ('bb',), ('cc',))
>>> import itertools
>>> list(itertools.chain(*tu))
['aa', 'bb', 'cc']

Edit Could you please comment on the cost tradeoff? (for loop and itertools)

Itertools is significantly faster:

>>> t = timeit.Timer(stmt="itertools.chain(*(('aa',), ('bb',), ('cc',)))")
>>> print t.timeit()
0.341422080994
>>> t = timeit.Timer(stmt="[a[0] for a in (('aa',), ('bb',), ('cc',))]")
>>> print t.timeit()
0.575773954391

Edit 2 Could you pl explain itertools.chain(*)

That * unpacks the sequence into positional arguments, in this case a nested tuple of tuples.

Example:

>>> def f(*args):
... print "len args:",len(args)
... for a in args:
... print a
...
>>> tu = (('aa',), ('bb',), ('cc',))
>>> f(tu)
len args: 1
(('aa',), ('bb',), ('cc',))
>>> f(*tu)
len args: 3
('aa',)
('bb',)
('cc',)

Another example:

>>> f('abcde')
len args: 1
abcde
>>> f(*'abcde')
len args: 5
a
b
c
d
e

See the documents on unpacking.

Convert a list of lists back into a list of tuples

tuple(myList) will convert myList into a tuple, provided that myList is something iterable like a list, a tuple, or a generator.

To convert a lists of lists in a list of tuples, using a list comprehension expression:

last_list = [tuple(x) for x in Long_list]

or, to also perform your string replacement:

last_list = [tuple(y.replace('/', '@') for y in x) for x in Long_list]

From Python's reference:

tuple( [iterable] )

Return a tuple whose items are the same and in the same order as iterable‘s items. iterable may be a sequence, a container that supports iteration, or an iterator object. If iterable is already a tuple, it is returned unchanged. For instance, tuple('abc') returns ('a', 'b', 'c') and tuple([1, 2, 3]) returns (1, 2, 3). If no argument is given, returns a new empty tuple, ().

tuple is an immutable sequence type, as documented in Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange. For other containers see the built in dict, list and [set] classes, and the collections module.



Related Topics



Leave a reply



Submit