Test whether list A is contained in list B












6















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
    15 hours ago











  • @TalhaIsrar No. Clarified the text.

    – Daniel Standage
    15 hours ago













  • are the elements in the list always unique?

    – Cyzanfar
    15 hours ago











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

    – Daniel Standage
    15 hours ago






  • 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
    15 hours ago
















6















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
    15 hours ago











  • @TalhaIsrar No. Clarified the text.

    – Daniel Standage
    15 hours ago













  • are the elements in the list always unique?

    – Cyzanfar
    15 hours ago











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

    – Daniel Standage
    15 hours ago






  • 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
    15 hours ago














6












6








6








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 15 hours ago







Daniel Standage

















asked 15 hours ago









Daniel StandageDaniel Standage

3,557135190




3,557135190













  • will the first list always be of length 3

    – Talha Israr
    15 hours ago











  • @TalhaIsrar No. Clarified the text.

    – Daniel Standage
    15 hours ago













  • are the elements in the list always unique?

    – Cyzanfar
    15 hours ago











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

    – Daniel Standage
    15 hours ago






  • 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
    15 hours ago



















  • will the first list always be of length 3

    – Talha Israr
    15 hours ago











  • @TalhaIsrar No. Clarified the text.

    – Daniel Standage
    15 hours ago













  • are the elements in the list always unique?

    – Cyzanfar
    15 hours ago











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

    – Daniel Standage
    15 hours ago






  • 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
    15 hours ago

















will the first list always be of length 3

– Talha Israr
15 hours ago





will the first list always be of length 3

– Talha Israr
15 hours ago













@TalhaIsrar No. Clarified the text.

– Daniel Standage
15 hours ago







@TalhaIsrar No. Clarified the text.

– Daniel Standage
15 hours ago















are the elements in the list always unique?

– Cyzanfar
15 hours ago





are the elements in the list always unique?

– Cyzanfar
15 hours ago













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

– Daniel Standage
15 hours ago





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

– Daniel Standage
15 hours ago




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
15 hours ago





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
15 hours ago












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
    14 hours ago





















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








New contributor




Dariush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




























    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
        14 hours ago













      • yup hope that helps!

        – Cyzanfar
        14 hours ago



















      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








      New contributor




      Talha Israr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




























        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










          New contributor




          PrMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.




























            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
                14 hours ago


















              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
                14 hours ago
















              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 14 hours ago

























              answered 15 hours ago









              AustinAustin

              9,7523828




              9,7523828













              • 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
                14 hours ago





















              • 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
                14 hours ago



















              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
              14 hours ago







              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
              14 hours ago















              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








              New contributor




              Dariush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

























                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








                New contributor




                Dariush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.























                  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








                  New contributor




                  Dariush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.










                  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








                  New contributor




                  Dariush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Dariush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 14 hours ago









                  DariushDariush

                  6310




                  6310




                  New contributor




                  Dariush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Dariush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Dariush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.























                      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 15 hours ago









                          abcabc

                          2,2851130




                          2,2851130























                              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
                                14 hours ago













                              • yup hope that helps!

                                – Cyzanfar
                                14 hours ago
















                              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
                                14 hours ago













                              • yup hope that helps!

                                – Cyzanfar
                                14 hours ago














                              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 15 hours ago









                              CyzanfarCyzanfar

                              4,31132246




                              4,31132246













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

                                – Daniel Standage
                                14 hours ago













                              • yup hope that helps!

                                – Cyzanfar
                                14 hours ago



















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

                                – Daniel Standage
                                14 hours ago













                              • yup hope that helps!

                                – Cyzanfar
                                14 hours ago

















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

                              – Daniel Standage
                              14 hours ago







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

                              – Daniel Standage
                              14 hours ago















                              yup hope that helps!

                              – Cyzanfar
                              14 hours ago





                              yup hope that helps!

                              – Cyzanfar
                              14 hours ago











                              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








                              New contributor




                              Talha Israr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.

























                                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








                                New contributor




                                Talha Israr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.























                                  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








                                  New contributor




                                  Talha Israr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.










                                  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








                                  New contributor




                                  Talha Israr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.









                                  share|improve this answer



                                  share|improve this answer






                                  New contributor




                                  Talha Israr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.









                                  answered 15 hours ago









                                  Talha IsrarTalha Israr

                                  1178




                                  1178




                                  New contributor




                                  Talha Israr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.





                                  New contributor





                                  Talha Israr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.






                                  Talha Israr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.























                                      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 14 hours ago









                                          newkidnewkid

                                          414215




                                          414215























                                              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










                                              New contributor




                                              PrMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.

























                                                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










                                                New contributor




                                                PrMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                Check out our Code of Conduct.























                                                  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










                                                  New contributor




                                                  PrMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.










                                                  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










                                                  New contributor




                                                  PrMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.









                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited 14 hours ago









                                                  Cyzanfar

                                                  4,31132246




                                                  4,31132246






                                                  New contributor




                                                  PrMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.









                                                  answered 15 hours ago









                                                  PrMiPrMi

                                                  112




                                                  112




                                                  New contributor




                                                  PrMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.





                                                  New contributor





                                                  PrMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.






                                                  PrMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.























                                                      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 14 hours ago









                                                          Peter WoodPeter Wood

                                                          16.3k33269




                                                          16.3k33269






























                                                              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

                                                              Bundesstraße 106

                                                              Verónica Boquete

                                                              Ida-Boy-Ed-Garten