Skip to content Skip to sidebar Skip to footer

Removing Brackets And Comma From A List Of Strings (python & Sqlite)

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 Zucke

Solution 1:

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

<iframeheight="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>

Solution 2:

Try This -

region=['INDIA','ME',"SEA","AFRICA","SAARC","LATIN AMERICA"]
print region
lst= str(region)
lst.strip("[").strip("]").strip("'")

Solution 3:

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']

Solution 4:

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]

Post a Comment for "Removing Brackets And Comma From A List Of Strings (python & Sqlite)"