fightstarr20
fightstarr20

Reputation: 12628

Python remove brackets and quotes from list item

I am using Python to return a list item by its index like this:

selectedtext = list_of_lists[44]
print("Selected Text Is")
print(selectedtext)

And it is returning this:

['My Results']

How can I remove the brackets and quotes from the result?

Upvotes: 0

Views: 8624

Answers (2)

Mark Skelton
Mark Skelton

Reputation: 3891

It looks like index 44 of list_of_lists is a list itself so you will need to get the first item in that list. You could do this:

selectedtext = list_of_lists[44]
print("Selected Text Is")
print(selectedtext[0])

Or:

selectedtext = list_of_lists[44][0]
print("Selected Text Is")
print(selectedtext)

Upvotes: 4

Martijn Pieters
Martijn Pieters

Reputation: 1125148

You appear to want to print the first item of your nested list only. Use indexing:

print(selectedtext[0])

You were printing a list, and lists, like other containers, include their contents with the repr() output for each; printing lists is meant for debugging mostly, not for end-user presentations.

If your list can contain multiple items, and you still want these items to be separated by comments, use str.join() to produce a new string built from the list contents:

print(', '.join(selectedtext))

Upvotes: 2

Related Questions