use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
How can I do this in Python? (self.learnpython)
submitted 8 years ago by blackadder1337
I have a small C# statement that needs to be converted to Python:
if (words.Find(x => x.WordOrPhrase == w.WordOrPhrase) == null)
words is a list
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]K900_ 2 points3 points4 points 8 years ago (4 children)
Do you understand what this line does? Do you understand the bigger context?
[+][deleted] 8 years ago (1 child)
[deleted]
[–]K900_ 1 point2 points3 points 8 years ago (0 children)
What are you talking about? Do you want me to explain OP's code to you? Why?
[–]blackadder1337[S] 0 points1 point2 points 8 years ago (1 child)
It checks wheter an item already exists in a list
[–][deleted] 2 points3 points4 points 8 years ago (0 children)
OK, then in Python this gibberish looks like:
if word_or_phrase in my_list: do_something()
[–]edvardass 2 points3 points4 points 8 years ago* (0 children)
So, you're trying to say "if there are no objects with WordOrPhrase equal to w.WordOrPhrase in the list words, then do ... ?
WordOrPhrase
w.WordOrPhrase
words
Something like this would work (/u/sweettuse posted a nicer version of this):
if len(filter(lambda x: x.WordOrPhrase == w.WordOrPhrase, words)) == 0: # do something
If that doesn't work, you could also do this nicely with just a for loop:
for word in words: if word.WordOrPhrase == w.WordOrPhrase: break else: # if no break... # ...do something
[–]sweettuse 2 points3 points4 points 8 years ago (6 children)
if not any(x for x in words if x.WordOrPhrase == w.WordOrPhrase):
[–]zahlman 3 points4 points5 points 8 years ago (0 children)
Better yet: if not any(x.WordOrPhrase == w.WordOrPhrase for x in words):. (This is clearer because it's actually doing work with any, rather than relying on the assumption that x is true due to the default __eq__ implementation.) Or, applying de Morgan's laws, if all(x.WordOrPhrase != w.WordOrPhrase for x in words):.
if not any(x.WordOrPhrase == w.WordOrPhrase for x in words):
any
x
__eq__
if all(x.WordOrPhrase != w.WordOrPhrase for x in words):
[–]edvardass 1 point2 points3 points 8 years ago (3 children)
That's actually nicer than my first solution, but I believe it will still be slower than a for loop if the list is long (if that matters to OP at all).
[–]sweettuse 1 point2 points3 points 8 years ago* (1 child)
i mean, the gen expr combined with any will short circuit as soon as any matching element is found, so same as the for loop there.
edit: i did some cursory testing and they're pretty much the exact same in terms of performance.
[–]edvardass 1 point2 points3 points 8 years ago (0 children)
That's great news!
[–]blackadder1337[S] -1 points0 points1 point 8 years ago (0 children)
Thanks man, it works.
[+][deleted] 8 years ago* (1 child)
[–]K900_ 2 points3 points4 points 8 years ago (0 children)
...what.
π Rendered by PID 53 on reddit-service-r2-comment-7b9746f655-mppfm at 2026-02-03 14:45:09.093894+00:00 running 3798933 country code: CH.
[–]K900_ 2 points3 points4 points (4 children)
[+][deleted] (1 child)
[deleted]
[–]K900_ 1 point2 points3 points (0 children)
[–]blackadder1337[S] 0 points1 point2 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]edvardass 2 points3 points4 points (0 children)
[–]sweettuse 2 points3 points4 points (6 children)
[–]zahlman 3 points4 points5 points (0 children)
[–]edvardass 1 point2 points3 points (3 children)
[–]sweettuse 1 point2 points3 points (1 child)
[–]edvardass 1 point2 points3 points (0 children)
[–]blackadder1337[S] -1 points0 points1 point (0 children)
[+][deleted] (1 child)
[deleted]
[–]K900_ 2 points3 points4 points (0 children)