Test whether list A is contained in list B












7















I have two lists, A & B, and I would like to test whether A is contained in B. By "contained" I mean that the elements of A appear in the exact same order within B with no other elements between them. What I'm looking for is very similar to the behavior of A in B if they were strings.



Some elements of A will be repeated. We can assume A will be shorter than B.



There are many answers to similar questions on SO, but most answer a different question:




  • Is A an element of B? (Not my question: B is a flat list, not a list of lists.)

  • Are all the elements of A contained in B? (Not my question: I'm concerned about order as well.)

  • Is A a sublist of B? (Not my question: I don't want to know whether the elements of A appear in the same order in B, I want to know if they appear exactly as they are somewhere in B.)


If the operation were implemented as the keyword containedin, it would behave like this.



>>> [2, 3, 4] containedin [1, 2, 3, 4, 5]
True
>>> [2, 3, 4] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 3, 4] containedin [5, 4, 3, 2, 1]
False
>>> [2, 2, 2] containedin [1, 2, 3, 4, 5]
False
>>> [2, 2, 2] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 2, 2] containedin [1, 1, 1, 2, 2, 2, 3, 3, 3]
True


Is there a concise way to perform this operation in Python? Am I missing some important terminology that would have led me to the answer more quickly?










share|improve this question

























  • will the first list always be of length 3

    – Talha Israr
    Jan 19 at 15:01











  • @TalhaIsrar No. Clarified the text.

    – Daniel Standage
    Jan 19 at 15:04













  • are the elements in the list always unique?

    – Cyzanfar
    Jan 19 at 15:05











  • @Cyzanfar No. Clarified the text again. :-)

    – Daniel Standage
    Jan 19 at 15:07






  • 1





    Um...someone want to explain their downvote? I put a lot of time into making it a clear question, and responding to requests for clarification.

    – Daniel Standage
    Jan 19 at 15:14
















7















I have two lists, A & B, and I would like to test whether A is contained in B. By "contained" I mean that the elements of A appear in the exact same order within B with no other elements between them. What I'm looking for is very similar to the behavior of A in B if they were strings.



Some elements of A will be repeated. We can assume A will be shorter than B.



There are many answers to similar questions on SO, but most answer a different question:




  • Is A an element of B? (Not my question: B is a flat list, not a list of lists.)

  • Are all the elements of A contained in B? (Not my question: I'm concerned about order as well.)

  • Is A a sublist of B? (Not my question: I don't want to know whether the elements of A appear in the same order in B, I want to know if they appear exactly as they are somewhere in B.)


If the operation were implemented as the keyword containedin, it would behave like this.



>>> [2, 3, 4] containedin [1, 2, 3, 4, 5]
True
>>> [2, 3, 4] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 3, 4] containedin [5, 4, 3, 2, 1]
False
>>> [2, 2, 2] containedin [1, 2, 3, 4, 5]
False
>>> [2, 2, 2] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 2, 2] containedin [1, 1, 1, 2, 2, 2, 3, 3, 3]
True


Is there a concise way to perform this operation in Python? Am I missing some important terminology that would have led me to the answer more quickly?










share|improve this question

























  • will the first list always be of length 3

    – Talha Israr
    Jan 19 at 15:01











  • @TalhaIsrar No. Clarified the text.

    – Daniel Standage
    Jan 19 at 15:04













  • are the elements in the list always unique?

    – Cyzanfar
    Jan 19 at 15:05











  • @Cyzanfar No. Clarified the text again. :-)

    – Daniel Standage
    Jan 19 at 15:07






  • 1





    Um...someone want to explain their downvote? I put a lot of time into making it a clear question, and responding to requests for clarification.

    – Daniel Standage
    Jan 19 at 15:14














7












7








7








I have two lists, A & B, and I would like to test whether A is contained in B. By "contained" I mean that the elements of A appear in the exact same order within B with no other elements between them. What I'm looking for is very similar to the behavior of A in B if they were strings.



Some elements of A will be repeated. We can assume A will be shorter than B.



There are many answers to similar questions on SO, but most answer a different question:




  • Is A an element of B? (Not my question: B is a flat list, not a list of lists.)

  • Are all the elements of A contained in B? (Not my question: I'm concerned about order as well.)

  • Is A a sublist of B? (Not my question: I don't want to know whether the elements of A appear in the same order in B, I want to know if they appear exactly as they are somewhere in B.)


If the operation were implemented as the keyword containedin, it would behave like this.



>>> [2, 3, 4] containedin [1, 2, 3, 4, 5]
True
>>> [2, 3, 4] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 3, 4] containedin [5, 4, 3, 2, 1]
False
>>> [2, 2, 2] containedin [1, 2, 3, 4, 5]
False
>>> [2, 2, 2] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 2, 2] containedin [1, 1, 1, 2, 2, 2, 3, 3, 3]
True


Is there a concise way to perform this operation in Python? Am I missing some important terminology that would have led me to the answer more quickly?










share|improve this question
















I have two lists, A & B, and I would like to test whether A is contained in B. By "contained" I mean that the elements of A appear in the exact same order within B with no other elements between them. What I'm looking for is very similar to the behavior of A in B if they were strings.



Some elements of A will be repeated. We can assume A will be shorter than B.



There are many answers to similar questions on SO, but most answer a different question:




  • Is A an element of B? (Not my question: B is a flat list, not a list of lists.)

  • Are all the elements of A contained in B? (Not my question: I'm concerned about order as well.)

  • Is A a sublist of B? (Not my question: I don't want to know whether the elements of A appear in the same order in B, I want to know if they appear exactly as they are somewhere in B.)


If the operation were implemented as the keyword containedin, it would behave like this.



>>> [2, 3, 4] containedin [1, 2, 3, 4, 5]
True
>>> [2, 3, 4] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 3, 4] containedin [5, 4, 3, 2, 1]
False
>>> [2, 2, 2] containedin [1, 2, 3, 4, 5]
False
>>> [2, 2, 2] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 2, 2] containedin [1, 1, 1, 2, 2, 2, 3, 3, 3]
True


Is there a concise way to perform this operation in Python? Am I missing some important terminology that would have led me to the answer more quickly?







python list contains






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 15:07







Daniel Standage

















asked Jan 19 at 15:00









Daniel StandageDaniel Standage

3,582135190




3,582135190













  • will the first list always be of length 3

    – Talha Israr
    Jan 19 at 15:01











  • @TalhaIsrar No. Clarified the text.

    – Daniel Standage
    Jan 19 at 15:04













  • are the elements in the list always unique?

    – Cyzanfar
    Jan 19 at 15:05











  • @Cyzanfar No. Clarified the text again. :-)

    – Daniel Standage
    Jan 19 at 15:07






  • 1





    Um...someone want to explain their downvote? I put a lot of time into making it a clear question, and responding to requests for clarification.

    – Daniel Standage
    Jan 19 at 15:14



















  • will the first list always be of length 3

    – Talha Israr
    Jan 19 at 15:01











  • @TalhaIsrar No. Clarified the text.

    – Daniel Standage
    Jan 19 at 15:04













  • are the elements in the list always unique?

    – Cyzanfar
    Jan 19 at 15:05











  • @Cyzanfar No. Clarified the text again. :-)

    – Daniel Standage
    Jan 19 at 15:07






  • 1





    Um...someone want to explain their downvote? I put a lot of time into making it a clear question, and responding to requests for clarification.

    – Daniel Standage
    Jan 19 at 15:14

















will the first list always be of length 3

– Talha Israr
Jan 19 at 15:01





will the first list always be of length 3

– Talha Israr
Jan 19 at 15:01













@TalhaIsrar No. Clarified the text.

– Daniel Standage
Jan 19 at 15:04







@TalhaIsrar No. Clarified the text.

– Daniel Standage
Jan 19 at 15:04















are the elements in the list always unique?

– Cyzanfar
Jan 19 at 15:05





are the elements in the list always unique?

– Cyzanfar
Jan 19 at 15:05













@Cyzanfar No. Clarified the text again. :-)

– Daniel Standage
Jan 19 at 15:07





@Cyzanfar No. Clarified the text again. :-)

– Daniel Standage
Jan 19 at 15:07




1




1





Um...someone want to explain their downvote? I put a lot of time into making it a clear question, and responding to requests for clarification.

– Daniel Standage
Jan 19 at 15:14





Um...someone want to explain their downvote? I put a lot of time into making it a clear question, and responding to requests for clarification.

– Daniel Standage
Jan 19 at 15:14












8 Answers
8






active

oldest

votes


















4














Use any with list slicing:



def contained_in(lst, sub):
n = len(sub)
return any(sub == lst[i:i+n] for i in range(len(lst)-n+1))


Or, use join to join both lists to strings and use in operator:



def contained_in(lst, sub):
return ','.join(map(str, sub)) in ','.join(map(str, lst))


Usage:



>>> contained_in([1, 2, 3, 4, 5], [2, 3, 4])
True
>>> contained_in([1, 2, 2, 4, 5], [2, 3, 4])
False





share|improve this answer


























  • I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1

    – Daniel Standage
    Jan 19 at 15:19





















3














many people have posted their answers. but I want to post my efforts anyway ;)
this is my code:



def containedin(a,b):
for j in range(len(b)-len(a)+1):
if a==b[j:j+len(a)]:
return True
return False

print(containedin([2, 3, 4],[1, 2, 3, 4, 5]))
print(containedin([2, 3, 4],[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]))
print(containedin([2, 3, 4],[5, 4, 3, 2, 1]))
print(containedin([2, 2, 2],[1, 2, 3, 4, 5]))
print(containedin([2, 2, 2],[1, 1, 1, 2, 2, 2, 3, 3, 3]))


this is the output:
True
False
False
False
True






share|improve this answer































    2














    Assuming a always shorter than b what you can do is as follows.



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





    share|improve this answer































      1














      Considering you need to preserve order:



      def contains(sub_array, array):
      for i in range(len(array)-len(sub_array)+1):
      for j in range(len(sub_array)):
      if array[i+j] != sub_array[j]:
      break
      else:
      return i, i+len(sub_array)
      return False





      share|improve this answer
























      • Nice. Even gives you the index of the first occurrence.

        – Daniel Standage
        Jan 19 at 15:15













      • yup hope that helps!

        – Cyzanfar
        Jan 19 at 15:16



















      0














      Use this function



      I tried to not make it complex



      def contains(list1,list2):

      str1=""
      for i in list1:
      str1+=str(i)

      str2=""
      for j in list2:
      str2+=str(j)

      if str1 in str2:
      return True

      else:
      return False


      Hope it works :)






      share|improve this answer































        0














        Something like this?



        class myList(list):
        def in_other(self, other_list):
        for i in range(0, len(other_list)-len(self)):
        if other_list[i:i+len(self)] == self:
        return True
        else:
        continue

        if __name__ == "__main__":

        x = myList([1, 2, 3])
        b = [0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]

        print(x.in_other(b))





        share|improve this answer































          0














          You can create the concatenate the 2 lists into two different strings. Then, write a function to check if one string is in another.



          def containedin(a, b):
          if b in a:
          return True
          return False`





          share|improve this answer

































            0














            No need to slice for every element:



            def contains(seq, sub):
            sub_length = len(sub)
            sub_first = sub[0]
            return any(sub == seq[index:index+sub_length]
            for index, element in enumerate(seq)
            if element == sub_first)


            Usage:



            >>> seq = [1, 2, 3, 4, 5]
            >>> sub = [2, 3, 4]

            >>> contains(seq, sub)
            True





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54268367%2ftest-whether-list-a-is-contained-in-list-b%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              8 Answers
              8






              active

              oldest

              votes








              8 Answers
              8






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4














              Use any with list slicing:



              def contained_in(lst, sub):
              n = len(sub)
              return any(sub == lst[i:i+n] for i in range(len(lst)-n+1))


              Or, use join to join both lists to strings and use in operator:



              def contained_in(lst, sub):
              return ','.join(map(str, sub)) in ','.join(map(str, lst))


              Usage:



              >>> contained_in([1, 2, 3, 4, 5], [2, 3, 4])
              True
              >>> contained_in([1, 2, 2, 4, 5], [2, 3, 4])
              False





              share|improve this answer


























              • I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1

                – Daniel Standage
                Jan 19 at 15:19


















              4














              Use any with list slicing:



              def contained_in(lst, sub):
              n = len(sub)
              return any(sub == lst[i:i+n] for i in range(len(lst)-n+1))


              Or, use join to join both lists to strings and use in operator:



              def contained_in(lst, sub):
              return ','.join(map(str, sub)) in ','.join(map(str, lst))


              Usage:



              >>> contained_in([1, 2, 3, 4, 5], [2, 3, 4])
              True
              >>> contained_in([1, 2, 2, 4, 5], [2, 3, 4])
              False





              share|improve this answer


























              • I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1

                – Daniel Standage
                Jan 19 at 15:19
















              4












              4








              4







              Use any with list slicing:



              def contained_in(lst, sub):
              n = len(sub)
              return any(sub == lst[i:i+n] for i in range(len(lst)-n+1))


              Or, use join to join both lists to strings and use in operator:



              def contained_in(lst, sub):
              return ','.join(map(str, sub)) in ','.join(map(str, lst))


              Usage:



              >>> contained_in([1, 2, 3, 4, 5], [2, 3, 4])
              True
              >>> contained_in([1, 2, 2, 4, 5], [2, 3, 4])
              False





              share|improve this answer















              Use any with list slicing:



              def contained_in(lst, sub):
              n = len(sub)
              return any(sub == lst[i:i+n] for i in range(len(lst)-n+1))


              Or, use join to join both lists to strings and use in operator:



              def contained_in(lst, sub):
              return ','.join(map(str, sub)) in ','.join(map(str, lst))


              Usage:



              >>> contained_in([1, 2, 3, 4, 5], [2, 3, 4])
              True
              >>> contained_in([1, 2, 2, 4, 5], [2, 3, 4])
              False






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 19 at 16:07

























              answered Jan 19 at 15:07









              AustinAustin

              10.3k3828




              10.3k3828













              • I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1

                – Daniel Standage
                Jan 19 at 15:19





















              • I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1

                – Daniel Standage
                Jan 19 at 15:19



















              I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1

              – Daniel Standage
              Jan 19 at 15:19







              I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1

              – Daniel Standage
              Jan 19 at 15:19















              3














              many people have posted their answers. but I want to post my efforts anyway ;)
              this is my code:



              def containedin(a,b):
              for j in range(len(b)-len(a)+1):
              if a==b[j:j+len(a)]:
              return True
              return False

              print(containedin([2, 3, 4],[1, 2, 3, 4, 5]))
              print(containedin([2, 3, 4],[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]))
              print(containedin([2, 3, 4],[5, 4, 3, 2, 1]))
              print(containedin([2, 2, 2],[1, 2, 3, 4, 5]))
              print(containedin([2, 2, 2],[1, 1, 1, 2, 2, 2, 3, 3, 3]))


              this is the output:
              True
              False
              False
              False
              True






              share|improve this answer




























                3














                many people have posted their answers. but I want to post my efforts anyway ;)
                this is my code:



                def containedin(a,b):
                for j in range(len(b)-len(a)+1):
                if a==b[j:j+len(a)]:
                return True
                return False

                print(containedin([2, 3, 4],[1, 2, 3, 4, 5]))
                print(containedin([2, 3, 4],[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]))
                print(containedin([2, 3, 4],[5, 4, 3, 2, 1]))
                print(containedin([2, 2, 2],[1, 2, 3, 4, 5]))
                print(containedin([2, 2, 2],[1, 1, 1, 2, 2, 2, 3, 3, 3]))


                this is the output:
                True
                False
                False
                False
                True






                share|improve this answer


























                  3












                  3








                  3







                  many people have posted their answers. but I want to post my efforts anyway ;)
                  this is my code:



                  def containedin(a,b):
                  for j in range(len(b)-len(a)+1):
                  if a==b[j:j+len(a)]:
                  return True
                  return False

                  print(containedin([2, 3, 4],[1, 2, 3, 4, 5]))
                  print(containedin([2, 3, 4],[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]))
                  print(containedin([2, 3, 4],[5, 4, 3, 2, 1]))
                  print(containedin([2, 2, 2],[1, 2, 3, 4, 5]))
                  print(containedin([2, 2, 2],[1, 1, 1, 2, 2, 2, 3, 3, 3]))


                  this is the output:
                  True
                  False
                  False
                  False
                  True






                  share|improve this answer













                  many people have posted their answers. but I want to post my efforts anyway ;)
                  this is my code:



                  def containedin(a,b):
                  for j in range(len(b)-len(a)+1):
                  if a==b[j:j+len(a)]:
                  return True
                  return False

                  print(containedin([2, 3, 4],[1, 2, 3, 4, 5]))
                  print(containedin([2, 3, 4],[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]))
                  print(containedin([2, 3, 4],[5, 4, 3, 2, 1]))
                  print(containedin([2, 2, 2],[1, 2, 3, 4, 5]))
                  print(containedin([2, 2, 2],[1, 1, 1, 2, 2, 2, 3, 3, 3]))


                  this is the output:
                  True
                  False
                  False
                  False
                  True







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 19 at 15:22









                  DariushDariush

                  76110




                  76110























                      2














                      Assuming a always shorter than b what you can do is as follows.



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





                      share|improve this answer




























                        2














                        Assuming a always shorter than b what you can do is as follows.



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





                        share|improve this answer


























                          2












                          2








                          2







                          Assuming a always shorter than b what you can do is as follows.



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





                          share|improve this answer













                          Assuming a always shorter than b what you can do is as follows.



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






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 19 at 15:07









                          abcabc

                          2,3351130




                          2,3351130























                              1














                              Considering you need to preserve order:



                              def contains(sub_array, array):
                              for i in range(len(array)-len(sub_array)+1):
                              for j in range(len(sub_array)):
                              if array[i+j] != sub_array[j]:
                              break
                              else:
                              return i, i+len(sub_array)
                              return False





                              share|improve this answer
























                              • Nice. Even gives you the index of the first occurrence.

                                – Daniel Standage
                                Jan 19 at 15:15













                              • yup hope that helps!

                                – Cyzanfar
                                Jan 19 at 15:16
















                              1














                              Considering you need to preserve order:



                              def contains(sub_array, array):
                              for i in range(len(array)-len(sub_array)+1):
                              for j in range(len(sub_array)):
                              if array[i+j] != sub_array[j]:
                              break
                              else:
                              return i, i+len(sub_array)
                              return False





                              share|improve this answer
























                              • Nice. Even gives you the index of the first occurrence.

                                – Daniel Standage
                                Jan 19 at 15:15













                              • yup hope that helps!

                                – Cyzanfar
                                Jan 19 at 15:16














                              1












                              1








                              1







                              Considering you need to preserve order:



                              def contains(sub_array, array):
                              for i in range(len(array)-len(sub_array)+1):
                              for j in range(len(sub_array)):
                              if array[i+j] != sub_array[j]:
                              break
                              else:
                              return i, i+len(sub_array)
                              return False





                              share|improve this answer













                              Considering you need to preserve order:



                              def contains(sub_array, array):
                              for i in range(len(array)-len(sub_array)+1):
                              for j in range(len(sub_array)):
                              if array[i+j] != sub_array[j]:
                              break
                              else:
                              return i, i+len(sub_array)
                              return False






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 19 at 15:11









                              CyzanfarCyzanfar

                              4,38932247




                              4,38932247













                              • Nice. Even gives you the index of the first occurrence.

                                – Daniel Standage
                                Jan 19 at 15:15













                              • yup hope that helps!

                                – Cyzanfar
                                Jan 19 at 15:16



















                              • Nice. Even gives you the index of the first occurrence.

                                – Daniel Standage
                                Jan 19 at 15:15













                              • yup hope that helps!

                                – Cyzanfar
                                Jan 19 at 15:16

















                              Nice. Even gives you the index of the first occurrence.

                              – Daniel Standage
                              Jan 19 at 15:15







                              Nice. Even gives you the index of the first occurrence.

                              – Daniel Standage
                              Jan 19 at 15:15















                              yup hope that helps!

                              – Cyzanfar
                              Jan 19 at 15:16





                              yup hope that helps!

                              – Cyzanfar
                              Jan 19 at 15:16











                              0














                              Use this function



                              I tried to not make it complex



                              def contains(list1,list2):

                              str1=""
                              for i in list1:
                              str1+=str(i)

                              str2=""
                              for j in list2:
                              str2+=str(j)

                              if str1 in str2:
                              return True

                              else:
                              return False


                              Hope it works :)






                              share|improve this answer




























                                0














                                Use this function



                                I tried to not make it complex



                                def contains(list1,list2):

                                str1=""
                                for i in list1:
                                str1+=str(i)

                                str2=""
                                for j in list2:
                                str2+=str(j)

                                if str1 in str2:
                                return True

                                else:
                                return False


                                Hope it works :)






                                share|improve this answer


























                                  0












                                  0








                                  0







                                  Use this function



                                  I tried to not make it complex



                                  def contains(list1,list2):

                                  str1=""
                                  for i in list1:
                                  str1+=str(i)

                                  str2=""
                                  for j in list2:
                                  str2+=str(j)

                                  if str1 in str2:
                                  return True

                                  else:
                                  return False


                                  Hope it works :)






                                  share|improve this answer













                                  Use this function



                                  I tried to not make it complex



                                  def contains(list1,list2):

                                  str1=""
                                  for i in list1:
                                  str1+=str(i)

                                  str2=""
                                  for j in list2:
                                  str2+=str(j)

                                  if str1 in str2:
                                  return True

                                  else:
                                  return False


                                  Hope it works :)







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Jan 19 at 15:11









                                  Talha IsrarTalha Israr

                                  470212




                                  470212























                                      0














                                      Something like this?



                                      class myList(list):
                                      def in_other(self, other_list):
                                      for i in range(0, len(other_list)-len(self)):
                                      if other_list[i:i+len(self)] == self:
                                      return True
                                      else:
                                      continue

                                      if __name__ == "__main__":

                                      x = myList([1, 2, 3])
                                      b = [0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]

                                      print(x.in_other(b))





                                      share|improve this answer




























                                        0














                                        Something like this?



                                        class myList(list):
                                        def in_other(self, other_list):
                                        for i in range(0, len(other_list)-len(self)):
                                        if other_list[i:i+len(self)] == self:
                                        return True
                                        else:
                                        continue

                                        if __name__ == "__main__":

                                        x = myList([1, 2, 3])
                                        b = [0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]

                                        print(x.in_other(b))





                                        share|improve this answer


























                                          0












                                          0








                                          0







                                          Something like this?



                                          class myList(list):
                                          def in_other(self, other_list):
                                          for i in range(0, len(other_list)-len(self)):
                                          if other_list[i:i+len(self)] == self:
                                          return True
                                          else:
                                          continue

                                          if __name__ == "__main__":

                                          x = myList([1, 2, 3])
                                          b = [0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]

                                          print(x.in_other(b))





                                          share|improve this answer













                                          Something like this?



                                          class myList(list):
                                          def in_other(self, other_list):
                                          for i in range(0, len(other_list)-len(self)):
                                          if other_list[i:i+len(self)] == self:
                                          return True
                                          else:
                                          continue

                                          if __name__ == "__main__":

                                          x = myList([1, 2, 3])
                                          b = [0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]

                                          print(x.in_other(b))






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Jan 19 at 15:16









                                          newkidnewkid

                                          725417




                                          725417























                                              0














                                              You can create the concatenate the 2 lists into two different strings. Then, write a function to check if one string is in another.



                                              def containedin(a, b):
                                              if b in a:
                                              return True
                                              return False`





                                              share|improve this answer






























                                                0














                                                You can create the concatenate the 2 lists into two different strings. Then, write a function to check if one string is in another.



                                                def containedin(a, b):
                                                if b in a:
                                                return True
                                                return False`





                                                share|improve this answer




























                                                  0












                                                  0








                                                  0







                                                  You can create the concatenate the 2 lists into two different strings. Then, write a function to check if one string is in another.



                                                  def containedin(a, b):
                                                  if b in a:
                                                  return True
                                                  return False`





                                                  share|improve this answer















                                                  You can create the concatenate the 2 lists into two different strings. Then, write a function to check if one string is in another.



                                                  def containedin(a, b):
                                                  if b in a:
                                                  return True
                                                  return False`






                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Jan 19 at 15:16









                                                  Cyzanfar

                                                  4,38932247




                                                  4,38932247










                                                  answered Jan 19 at 15:13









                                                  PrMiPrMi

                                                  112




                                                  112























                                                      0














                                                      No need to slice for every element:



                                                      def contains(seq, sub):
                                                      sub_length = len(sub)
                                                      sub_first = sub[0]
                                                      return any(sub == seq[index:index+sub_length]
                                                      for index, element in enumerate(seq)
                                                      if element == sub_first)


                                                      Usage:



                                                      >>> seq = [1, 2, 3, 4, 5]
                                                      >>> sub = [2, 3, 4]

                                                      >>> contains(seq, sub)
                                                      True





                                                      share|improve this answer




























                                                        0














                                                        No need to slice for every element:



                                                        def contains(seq, sub):
                                                        sub_length = len(sub)
                                                        sub_first = sub[0]
                                                        return any(sub == seq[index:index+sub_length]
                                                        for index, element in enumerate(seq)
                                                        if element == sub_first)


                                                        Usage:



                                                        >>> seq = [1, 2, 3, 4, 5]
                                                        >>> sub = [2, 3, 4]

                                                        >>> contains(seq, sub)
                                                        True





                                                        share|improve this answer


























                                                          0












                                                          0








                                                          0







                                                          No need to slice for every element:



                                                          def contains(seq, sub):
                                                          sub_length = len(sub)
                                                          sub_first = sub[0]
                                                          return any(sub == seq[index:index+sub_length]
                                                          for index, element in enumerate(seq)
                                                          if element == sub_first)


                                                          Usage:



                                                          >>> seq = [1, 2, 3, 4, 5]
                                                          >>> sub = [2, 3, 4]

                                                          >>> contains(seq, sub)
                                                          True





                                                          share|improve this answer













                                                          No need to slice for every element:



                                                          def contains(seq, sub):
                                                          sub_length = len(sub)
                                                          sub_first = sub[0]
                                                          return any(sub == seq[index:index+sub_length]
                                                          for index, element in enumerate(seq)
                                                          if element == sub_first)


                                                          Usage:



                                                          >>> seq = [1, 2, 3, 4, 5]
                                                          >>> sub = [2, 3, 4]

                                                          >>> contains(seq, sub)
                                                          True






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Jan 19 at 15:29









                                                          Peter WoodPeter Wood

                                                          16.4k33270




                                                          16.4k33270






























                                                              draft saved

                                                              draft discarded




















































                                                              Thanks for contributing an answer to Stack Overflow!


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid



                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.


                                                              To learn more, see our tips on writing great answers.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function () {
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54268367%2ftest-whether-list-a-is-contained-in-list-b%23new-answer', 'question_page');
                                                              }
                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              Probability when a professor distributes a quiz and homework assignment to a class of n students.

                                                              Aardman Animations

                                                              Are they similar matrix