0

I have a list: list = ['abd','def','gab','dab']

And I want to check if a certain element say x = 'gab' is equal to an element in the list.

If so, then I want to do something, otherwise I want to pass. And at the end of the for loop, if x was not equal to any element in list, then I want to run some batch of code.

Here is my code:

for string in range(len(list)): 
    if list(string) ==x: 
        do something...
        break
    else: 
        pass

The challenge I have is to write the condition to check if x is not equal to any element in list- how can I write this condition?

Intuitively, I would write this code after the for loop- but the issue is that it will then execute regardless of whether x is equal to an element in list or not, and I don't want this, as I only want this next block of code to execute if x was not equal to an element in list! I hope this makes sense.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
if fhhf
  • 119
  • 6
  • In your own words, when you write `range(len(list))`, what do you expect that to mean? What do you think will be the first value generated for `string`? The second etc.? – Karl Knechtel Jul 26 '21 at 10:48

2 Answers2

2

In this case, you can use: if x in list:

In more complex situations, if you have something more complex than a simple comparison, you could do: if any(x == item for item in list):

As a side note, it's not a good idea to use the name list, because Python already uses that name and it could get confusing.

PS: To add another solution, in Python a for loop can have an else clause, which is executed if there was no break; that will be useful if the body of the loop is too complex to fit into a single expression.

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
  • Hi Sabik, thanks. I initially used 'if x in list' but this doesn't work, because if let's say x = 'ab' it would return True! Because 'ab' is in 'gab', but they are not equal! – if fhhf Jul 26 '21 at 10:48
  • But I believe your second recommendation works just fine. – if fhhf Jul 26 '21 at 10:48
  • Nope, it won't match unless the whole item is equal – Jiří Baum Jul 26 '21 at 10:49
  • Is any() commonly used? It is the first time I've come across it – if fhhf Jul 26 '21 at 10:50
  • 1
    "Because 'ab' is in 'gab'" This will **not** cause the code to behave the way you expect. `in`, with a list, compares for *equality* with *each element*. It is only `in` with a *single string* that does a substring match. – Karl Knechtel Jul 26 '21 at 10:50
  • `any` is extremely common, but most tutorials think it's too advanced for new programmers. They are wrong about this, but understandably wrong because they don't follow the right path to build up to it (teaching about list comprehensions and generator expressions, which in normal cases are *easier* to understand than the corresponding `for` loops). Yes, I'm quite dogmatic about this. – Karl Knechtel Jul 26 '21 at 10:51
  • Thanks a lot Karl- I've come across the aforementioned issues in many forms, and using any() solves a lot of the problems using this kind of logic. I am familiar with list comprehensions and agree they are easier to understand than for loops, even though I started with learning for loops as they are used more commonly. – if fhhf Jul 26 '21 at 10:54
  • Note that there is a subtle difference, but it would only affect items that are not equal to themselves; for instance, if we set `x = float('NaN'); list1 = [x]` then `x in list1` is different to `any(x == item for item in list1)`. That's a weird and obscure corner case, though. – Jiří Baum Jul 26 '21 at 10:56
  • BTW, there's also a corresponding `all()` function – Jiří Baum Jul 26 '21 at 10:57
  • PS: Another solution is to use an `else` clause on the `for` loop... – Jiří Baum Jul 26 '21 at 11:04
0

You can use any()

x = 'gab'
list1 = ['abd','def','gab','dab']
if any(x==item for item in list1):
    print("Yes. Match found.")
else:
    print("Match was not found")