2

Trying to make list 1, into list 2 shown in the code below by removing the brackets and commas within the brackets so I can use the strings for SQLite select queries:

[('Mark Zuckerberg',), ('Bill Gates',), ('Tim Cook',), ('Wlliam Sidis',), ('Elon Musk',)]
['Mark Zuckerberg', 'Bill Gates', 'Tim Cook', 'William Sidis', 'Elon Musk']
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Perse
  • 31
  • 1
  • 1
  • 3
  • 3
    `[thing[0] for thing in list1]`? You have a list of tuples. You're not *"removing brackets and comma"*, you're extracting the first value from the tuple. – jonrsharpe Mar 01 '18 at 09:56

4 Answers4

2

While fetching row and storing in list use str(row)

list=[('Mark Zuckerberg',), ('Bill Gates',), ('Tim Cook',), ('Wlliam Sidis',), ('Elon Musk',)]
listoutput=[i[0] for i in list]
print(listoutput)

Check output below

<iframe height="400px" width="100%" src="https://repl.it/repls/PortlyCarefulCodegeneration?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>
Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27
2

Try This -

region=['INDIA','ME',"SEA","AFRICA","SAARC","LATIN AMERICA"]
print region
lst= str(region)
lst.strip("[").strip("]").strip("'")
Jaydip Rakholiya
  • 792
  • 10
  • 20
Akshay Lande
  • 87
  • 1
  • 7
1

Suppose you have a list like this one,

animal_raw = [('cat', ), ('dog', ), ('elephant', )]

And now we will convert it into the one you asked that is without commas and parenthesis.

animal = [i[0] for i in animal_raw]

Now , print(animal). You should now get the output,

['cat', 'dog', 'elephant']
Nikhil.Nixel
  • 555
  • 2
  • 11
  • 25
0

from a tuple we can access the element by index as tuple[index]

single element tuples python represent as

(element,)

you have list of tuples

a = [('Mark Zuckerberg',), ('Bill Gates',), .... ]
b=[]
for i in a :
    b.append(i[0])

print(b)

or short version

b = [i[0] for i in a]