Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add quotes to every single entry of a list

I have a list that goes like

Foo 6
Bar 9
Ding 8
Dong 7
...

I used some online tool to add commas to each entry, but if I try to create a list in python, i get a syntax error

list=[Foo 6, Bar 9, Ding 8, Dong 7,...]

I assume this is because i have strings and integers mixed, so adding quotes does work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

list=["Foo 6, Bar 9, Ding 8, Dong 7,..."]

But this has only one entry, so its useless. Is there any way to convert this into

list=["Foo 6", "Bar 9", "Ding 8", "Dong 7",...]

Thanks for the help

>Solution :

I always use below when i need to create a list from a table copied values

string = """Foo 6
Bar 9
Ding 8
Dong 7"""

for i in string.split("\n"):
    print(f"\"{i.strip()}\"", end=",")

Now the output is

"Foo 6","Bar 9","Ding 8","Dong 7",

Just Copy and paste to variable

After reading comment if you want a list variable you can run below

string = '''Foo 6
Bar 9
Ding 8
Dong 7'''

lis = []
for i in string.split("\n"):
    lis.append(i.strip())

print(lis)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading