-2

I was looking at this answer, which shows how to identify if the list has any consecutive equal elements.

In a given list lst = [1, 2, 3, 4, 5, 5, 6]

any(i==j for i,j in zip(lst, lst[1:])) will return True since two 5s are equal neighbors. However, this piece of code checks for any consecutive equal elements.

How do I change this code to check for a specific element, say 5, in the given list?

martineau
  • 119,623
  • 25
  • 170
  • 301
Isaac
  • 273
  • 3
  • 19

1 Answers1

0

You could use any(i==j==5 for i,j in zip(lst, lst[1:])) to check for 5's.

A___
  • 83
  • 5