How to determine if two given numbers are adjacent in a list with Python

2 Answers

0 votes
# Adjacent = two numbers that are next to each other in a list

lst = [3, 8, 0, 4, 7, 2, 9, 1]
a = 2 
b = 9

adjacent = any((lst[i], lst[i + 1]) == (a,b) for i in range(len(lst) - 1))

print(adjacent)

 

  
'''
run:
  
True
  
'''

 



answered Feb 1, 2024 by avibootz
0 votes
# Adjacent = two numbers that are next to each other in a list

lst = [3, 8, 0, 4, 7, 2, 9, 1]
a = 2 
b = 9

adjacent = (a, b) in zip(lst[:-1], lst[1:])

print(adjacent)

 

  
'''
run:
  
True
  
'''

 



answered Feb 1, 2024 by avibootz
...