简体   繁体   中英

How to remove apostrophes from a list in python?

I need to remove apostrophes -> ' <- from a list, within python, without using any add-ons, so only built in functions.

Eg I need a list like:

lista = ['a', 'b', 'c', 'd']

into

lista = [ a, b, c, d]

I've tried using for with.replace or making the list into a string then replacing, but I haven't had anything work yet.

Any chance you could help?

You cannot remove apostrophes from a list because that's pretty much how you identify what a list is. However the apostrophes will not interfere with anything you do with the list. The apostrophes show that the elements are strings.

You can use str.join() to concatenate strings with a specified separator.

For example:

>>> strings = ['A', 'B', 'C']
>>> print(', '.join(strings))
A, B, C

Furthermore, in your case, str.format() may also help:

>>> strings = ['A', 'B', 'C']
>>> print('strings = [{}]'.format(', '.join(strings)))
strings = [A, B, C]

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