1

I'm curious to know the most "pythonic" way to check if there is an item in a list of strings that contains a given substring.

For example, say we have a list of email addresses:

['email1@abc.com', 'anotheremail@grandmasSMTPserver.net', 'myboss@dontemailme.org']

and we need to send an email to most of the emails in this list, but not all of them. What is the simplest (read: most "pythonic") way to check for a list element that contains the substring, say, 'dontemailme.org' and then remove it from the list?

I'm mainly concerned with determining whether an item in the list contains the substring, ideally which item in particular, so that I can make corresponding adjustments to the list.

I come from a C++ background so my initial thought is to use a for loop with an if statement to check but I am often surprised at the flexibility of Python.

McFizz
  • 3,013
  • 3
  • 18
  • 26
  • 1
    You describe 2 different problems to solve. Are you trying to determine _if any_ item in the list contains the substring, e.g. return `True` in the example you gave, or just get all of the items that _don't_ contain the substring? – G. Anderson Apr 23 '20 at 20:25
  • @G.Anderson `"and then remove it from the list"` – DeepSpace Apr 23 '20 at 20:26
  • @DeepSpace yes, but that's why I asked for clarification (albeit poorly) of whether the question in the title need to be part of the output as well "check if a string that contains a given substring is present in list" – G. Anderson Apr 23 '20 at 20:31
  • @G.Anderson sorry for lack of clarity. The bit about removing the list item just applies to my situation. I'm mainly concerned with constructing a boolean statement that returns whether an item in the list contains the substring, ideally which item. Hopefully that helps – McFizz Apr 23 '20 at 20:36
  • Does this answer your question? [How exactly does the python any() function work?](https://stackoverflow.com/questions/16505456/how-exactly-does-the-python-any-function-work) – G. Anderson Apr 23 '20 at 20:41
  • As in the linked duplicate: `any('dontemailme.org' in i for i in['email1@abc.com', 'anotheremail@grandmasSMTPserver.net', 'myboss@dontemailme.org'])` – G. Anderson Apr 23 '20 at 20:42
  • That's very close to what I was looking for, but not quite as precise as I'd like. I've edited my question to hopefully be more precise about what I'd like to know. There have already been several helpful answers posted as well. – McFizz Apr 23 '20 at 20:58

3 Answers3

1

You can use list comprehension:

emails = ['email1@abc.com', 'anotheremail@grandmasSMTPserver.net', 'myboss@dontemailme.org']
output_list = [email for email in emails if 'dontemailme.org' not in email]

print(output_list) # output: ['email1@abc.com', 'anotheremail@grandmasSMTPserver.net']

Gabio
  • 9,126
  • 3
  • 12
  • 32
1

filter is one way:

filtered = filter(lambda email: 'dontemailme.org' not in email, emails)
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • With the caveat that while the input is a list, the output is not. – 9769953 Apr 23 '20 at 20:25
  • @00 which is a benefit since no new (probably needless) list is created. OP will probably iterate over these any, so `for email in filtered` will work just as well. Any kind of iteration will work actually – DeepSpace Apr 23 '20 at 20:27
  • I know that, but for a beginner, just printing `filtered` may actually be confusing. – 9769953 Apr 23 '20 at 20:33
  • This is a very interesting approach! – McFizz Apr 23 '20 at 20:42
0

I would use a list comprehension:

emails = ['email1@abc.com', 'anotheremail@grandmasSMTPserver.net', 'myboss@dontemailme.org']    
filtered_emails = [email for email in emails if "dontemailme.org" not in email]
9769953
  • 10,344
  • 3
  • 26
  • 37