0

I am trying to find a way to detect a sequence of strings in a list.

I.E.: for the list

List1 = ["A", "B", "C", "D", "E"],

how do I write a program that detects the sequence

"A", "B", "C" and returns / prints "True"? 

Thank you!

List1 = ["A", "B", "C", "D", "E"]
Axiom = "A", "B" (I am aware that this is a tuple, and therefore would not be detected)

for item in List1:
    if List1[0]==Axiom:
          print("True")

Expected Output:

True
Peter B
  • 22,460
  • 5
  • 32
  • 69
Asum
  • 1
  • 2
  • According to the Online Historical Encyclopaedia of Programming Languages, people have created about 8,945 coding languages. Today, various sources report anywhere from 250-2,500 coding languages, although far fewer rank as top contenders in the commonly used group. **Which of these are you talking about?** – Peter B Feb 20 '23 at 00:01
  • I am working with Python. – Asum Mar 14 '23 at 15:09
  • Does this answer your question? [Understanding slicing](https://stackoverflow.com/questions/509211/understanding-slicing) – ti7 Mar 14 '23 at 22:36

1 Answers1

0

You might try this:

def find_sequence(l,s):
   return ''.join(s) in ''.join(l)

The function transforms both the list and the sequence into strings and checks if the sequence is in the list. Notice that it works as intended only if the sequence you want to find is not interspersed inside the list.

List1  = ["A", "B", "C", "D", "E"]
Axiom  = "A", "B"

find_sequence(List1,Axiom)

And the output is

True