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 7018831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:06:12+00:00 2026-05-27T23:06:12+00:00

I am using Python 3.x. If I want to see if there are 8

  • 0

I am using Python 3.x.

If I want to see if there are 8 numbers in any of three lists how would I do that in Python.

If I have:

list1 = [5, 6, 7, 2, 3, 5]
list2 = [2, 5, 8, 9, 1, 5]
list3 = [4, 6, 7 ,9, 2, 3]

How would I write this correctly in python?:

if 5 and 6 and 3 in (lists 1, 2, and 3)

The part in parenthesis is obviously not correct. How would I write the correct syntax for this in Python?

  • 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-27T23:06:12+00:00Added an answer on May 27, 2026 at 11:06 pm

    This is pretty simple and quite obvious to do. If you want to check if any number is in any list, you do this:

    def any_number_in_any_lists(numbers, lists):
        for alist in lists:
            for number in numbers:
                 if number in alist:
                      return True
        return False
    

    If you want to check that all numbers are in all lists, you do this:

    def all_number_in_all_lists(numbers, lists):
        for alist in lists:
            for number in numbers:
                 if number not in alist:
                      return False
        return True
    

    If you want to check for all numbers in any lists, it gets more complicated. The easiest and clearer is to have two methods:

    def all_numbers_in_list(l, numbers):
        for n in numbers:
            if n is not in l:
                 return False
        return True
    
    def all_numbers_in_any_list(lists, numbers):
        for l in lists:
            if all_numbers_in_list(l, numbers):
                return True
        return False            
    

    And you’d use the functions above like this:

    if all_number_in_all_lists([3,5,6], [list1, list2, list3]):
        do something
    else:
        do something else
    

    Since you want less code, there are various “shortcuts” you can use. The shortcuts can make the code shorter, but will also make it less clear and harder to read, and hence they are not really a better choice. For example you can use the any function, and a generator expression:

     def any_number_in_any_lists(numbers, lists):
         for alist in lists:
             if any(number in alist for number in numbers):
                 return True
         return False
    

    You can even nest two of them:

    def any_number_in_any_lists(numbers, lists):
        return any(any(number in alist for number in numbers) for alist in lists)
    

    But understanding that code takes a long time and much more brain power. It is also significantly slower, as it will go through all lists, even if it finds a match in the first one. I can’t think of any way that is not slower at the moment, but there may be one. But you are unlikely to find anything that is significantly faster and not significantly more confusing.

    For the case of checking that all numbers are in all lists, you would do this:

    def all_number_in_all_lists(numbers, lists):
        return all(all(number in alist for number in numbers) for alist in lists)
    

    Which also, for some reason takes twice the time compared with the function above, even though there will be no shortcuts. It may have to do with the creation of intermediary lists.

    This is often the case with Python: Simple is best.

    Line by line explanation of the code you used, since you asked for it:

    def all_numbers_in_list(l, numbers):
    

    Defines a function, called “all_numbers_in_list”, with the parameters “l” and “numbers”.

        for n in numbers:
    

    For every item in numbers, assign variable “n” to that number, and then do the following block.

            if n is not in l:
    

    If the value of variable “n” is not in the list “l”.

                 return False
    

    Exit the function with False as return value.

        return True
    

    Exit the function with True as return value.

    def all_numbers_in_any_list(lists, numbers):
    

    Defines a function, called “all_numbers_in_any_list”, with the parameters “lists” and “numbers”.

        for l in lists:
    

    For every item in lists, assign variable “l” to that number, and then do the following block.

            if all_numbers_in_list(l, numbers):
    

    If calling the function “all_numbers_in_list” with the parameters “l” and “numbers” return True, do the following block:

                return True
    

    Exit the function with True as return value.

        return False 
    

    Exit the function with False as return value.

    Did that help?

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

Sidebar

Related Questions

I want to see if there is a microphone active using Python. How can
I want to develop a GAE application using python, but I fear that Google
Is there any way, in PostgreSQL accessed from Python using SQLObject, to create a
I have a I just want to understand it question.. first, I'm using python
Using Python I want to randomly rearrange sections of a string based on a
Using Python, I want to know whether Java is installed.
I want to access the clipboard using Python 3.1. I've obviously come across win32clipboard,
I want to develop a desktop application using python with basic crud operation. Is
I want to build nested json object using python jsonpickle, something like this {key:
I want to programmatically modify a bitmap using python but don't really need a

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.