Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Python, I am trying to add single quotation marks to elements containing a space in a list. I know how to add single quotes to all elements in a list. However, I have no idea how to add single quotes to elements containing a space only.

For example, I would like to change A to A'.

A = [hello, my name, is, Mike] 


A' = [hello, 'my name', is, Mike] 


What I have tried:

I've found some ways for a string, but couldn't find a way for elements in a list.

Please help me to solve this issue!
Posted
Updated 17-Feb-20 21:15pm

1 solution

Assuming (otherwise you get a syntax error) your input list is actually
Python
A = ["hello", "my name", "is", "Mike"]
then you might write
Python
B = map( lambda s : s  if s.find(" ") == -1 else "'" + s + "'", A)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900