TransWikia.com

How to convert a list made up of one tuple into a two item list?

Stack Overflow Asked by Python learner 93 on November 12, 2021

I have a function that returns a one item list, like so:

list = [('array_1','array_2')]

I want to change this so that the list is instead a two item one, without the parentheses or single quotes:

list = [array_1,array_2]

What would be the best way to go about doing this?

4 Answers

You could just typecast like this.

list = list([('array_1','array_2')][0])

Answered by marvincyk on November 12, 2021

You can try this:

lst_tuple = [('array_1', 'array_2')]
lst = []
for i in lst_tuple[0]:
    lst.append(i)

By iteratating over the list that contains the tuple and appending each item to a new list, you can get this result:

['array_1', 'array_2']

Answered by LoneLegend on November 12, 2021

Use chain.from_iterable

from itertools import chain

list(chain.from_iterable([('array_1','array_2')]))

['array_1', 'array_2']

Answered by sushanth on November 12, 2021

Try this

 lists = [('array_1','array_2')]
print([y for x in lists for y in x])

output

['array_1', 'array_2']

Answered by Umutambyi Gad on November 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP