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']
1
  • 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. Commented Mar 1, 2018 at 9:56

4 Answers 4

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>

Sign up to request clarification or add additional context in comments.

Comments

2

Try This -

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

Comments

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

1 Comment

This is the best solution I've found so far. Thank you.
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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.