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:

['[email protected]', '[email protected]', '[email protected]']

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.

7
  • 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? Commented Apr 23, 2020 at 20:25
  • @G.Anderson "and then remove it from the list" Commented Apr 23, 2020 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" Commented Apr 23, 2020 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 Commented Apr 23, 2020 at 20:36
  • Does this answer your question? How exactly does the python any() function work? Commented Apr 23, 2020 at 20:41

3 Answers 3

1

You can use list comprehension:

emails = ['[email protected]', '[email protected]', '[email protected]']
output_list = [email for email in emails if 'dontemailme.org' not in email]

print(output_list) # output: ['[email protected]', '[email protected]']

Sign up to request clarification or add additional context in comments.

Comments

1

filter is one way:

filtered = filter(lambda email: 'dontemailme.org' not in email, emails)

4 Comments

With the caveat that while the input is a list, the output is not.
@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
I know that, but for a beginner, just printing filtered may actually be confusing.
This is a very interesting approach!
0

I would use a list comprehension:

emails = ['[email protected]', '[email protected]', '[email protected]']    
filtered_emails = [email for email in emails if "dontemailme.org" not in email]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.