Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4036194
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:11:36+00:00 2026-05-20T12:11:36+00:00

I have to check if list1 is contained in list2. It also should check

  • 0

I have to check if list1 is contained in list2. It also should check if it appears in that order in the list as well. If it’s true, it should return true and false if it doesn’t.

def check(lst1, lst2):
for x in lst1:
    for y in lst2:
        xx = lst1.sort()
        yy = lst2
        if xx != yy:
            return False
        else:
            return True

I’m confusing myself with the for loops and also, I don’t know where to go from here to fix my code. Pointers please?

example of what it should do:

  check([4,0],[9,1,4,6,8,9])
  True
  check([1,2],[2,3,1])
  False
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:11:36+00:00Added an answer on May 20, 2026 at 12:11 pm

    I thought the problem was begging to be solved recursively, so I did:

    def check(needle, haystack):
      if not needle:
          return True
      try:
        offset = haystack.index(needle[0])
        return check(needle[1:], haystack[offset+1:])
      except:
        return False
    

    Edit:

    Brief explanation:

    First we check if the list we’re looking for is empty (this is important once we start calling ourselves), since all lists have the empty list inside it, we return True. Then we try finding the first item of the list we’re looking for in the list we’re looking at. If we find it, we then call the function again, but change the arguments a bit: we already looked at the first item of the ‘needle’ and saw it in the ‘haystack’. So now we need to check if the remainder of ‘needle’ is in the remainder of ‘haystack’. So we call the function again only with the remainder of both lists. The remainder of needle is everything but the first element, and the remainder of haystack is all items after the one we found. If we get to a point where the first list is empty, it means we found it in the haystack. If we get an exception, what we were looking for wasn’t found, so we return False, which bubbles up the call stack and gets returned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a check box list in that user can check or uncheck the
I have a list of check boxes. For the check boxes that are selected,
I would like for the generate List view contents to have a check added
I have an ASP:Repeater Which I would like to display a list of check
I have a list of 50,000 paths and I need to check if a
I have a simple list view with some check boxes in an alert dialog.
I have a form with a list of people with check boxes next to
I have a list of e-mails from one database which I want to check
I have a list in Python, and I want to check if any elements
I have a list of dictionaries. I need to check if all the dictionaries

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.