简体   繁体   中英

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?

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)

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM