Here is my list:
[Volume:vol-b81a2cb0, Volume:vol-ab2b1ba3, Volume:vol-fc2c1cf4]
I want it to look like this:
['vol-b81a2cb0', 'vol-ab2b1ba3', 'vol-fc2c1cf4']
So the following should be done:
Volume:
prefix must be removed from the list elements. try:
strList = map( str, objList)
strList = map( lambda x: x.replace( 'Volume:', ''), strList)
You could reduce @Marek code to one line:
strList = list(map(lambda x: str(x).replace('Volume:', ''), strList))
or just use list comprehension :
new_list = [str(i).replace( 'Volume:', '') for i in your_list]
Since Python 3.9 you can use removeprefix()
function what will simplify your code even more:
new_list = [str(i).removeprefix('Volume:') for i in your_list]
More info about removeprefix()
: PEP 616
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.