-1

I have the following problem:

I have a string that contains multiple E-Mail adresses. These adresses are not static. I pull them from my database. So for example when the adresses are pulled and I print the string the output is:

mails = 'email1', 'email2', 'email3'

Now I want to make a list out of the string. So my code is:

list = [mails]

But when I print the list, I get the following result:

["'email1', 'email2', 'email3'"]

How can I remove the double quotes, so that the output looks like this?

['email1', 'email2', 'email3']

Thank you for your answers :)

Zekra
  • 1
  • 1
  • 1
    When you print a list object, the list calls [`repr()`](https://docs.python.org/3/library/functions.html#repr) on each inner object. It is repr() that is causing the double quotes. This is only a matter of display and doesn't affect the contents of the original string. – Nayuki Jan 14 '22 at 07:21
  • Welcome to Stack Overflow. It is not clear what you mean. Are you saying that you have a variable named `mails`, that gets a value from your program? And when you do `print(mails)`, the result that you see on screen is **exactly** `'email1', 'email2', 'email3'`? I.e. you do **not** see the `mails = ` part when you use `print`? – Karl Knechtel Jan 14 '22 at 08:20

4 Answers4

1

Supposing you have, in your code:

mails = 'email1', 'email2', 'email3'

That is a tuple of strings, i.e.

('email1', 'email2', 'email3')

You can simply convert that into a list by casting:

mails = list(mails)

which will produce

['email1', 'email2', 'email3']

Of course, all depends on your input data. Normally you would retrieve the data from your database and add it instantly to a list or some data structure that supports the intended functionality on that data better than a list.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
ferdy
  • 7,366
  • 3
  • 35
  • 46
1

Using Regex and list comprehension:

import re
my_list = [re.sub(r"'([^']+)'", r"\1", x) for x in my_list]

Explanation: What is inside single quotes gets added to group which the replacement string \1 is referring to.

phk
  • 2,002
  • 1
  • 29
  • 54
0

Just Do this:

mails = 'email1', 'email2', 'email3'
mails= list(mails)
print(mails)

and now the result will be the same as you want:

['email1', 'email2', 'email3']
araz malek
  • 21
  • 4
-1
import json
a_list = json.dumps(emails)

Is how i would do it If i wanted a string representation like what is described...

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179