Python Forum
How to check if a list is in another list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if a list is in another list
#1
Hi All,

I have the below code:

list_1 = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

win_1 = ["1", "2", "3", "4"]

if win_1 in list_1:
    print("yes")
It doesn't print anything, how do I check if a list is in another list?
"Only Boring People Get Bored"
Reply
#2
Not sure what you want but, this will print anything from win_1 that is in list_1
list_1 = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
win_1 = ["1", "2", "3", "4"]

for item in list_1:
    if item in win_1:
        print(item)
Output:
1 2 3 4
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
list_1 = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
win_1 = ["1", "2", "3", "4"]
win_2 = ['1', '12']

print(all(item in list_1 for item in win_1))
print(all(item in list_1 for item in win_2))
Output:
True False
BashBedlam likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Membership testing in a set is faster than membership testing in a list. However, converting list to set also costs some time. So it's up to specific conditions and terms what to prefer.

list_1 = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
win_1 = ["1", "2", "3", "4"]

print(set(win_1).issubset(list_1))              # are all win_1 elements in list_1
print(set(win_1).intersection(list_1))         # what elements of win_1 in list_1
Output:
True {'4', '3', '2', '1'}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Looks like you're asking about a subsequence. You can check if a slice of a list matches your subsequence.

list_1 = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
 
win_1 = ["1", "2", "3", "4"]
 
if any(win_1 == list_1[x:x+len(win_1)] for x in range(len(list_1) - len(win_1) + 1)):
    print("yes")
List slicing and list comparison can take a while. So this can be expensive as the lists get long.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 394 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  [solved] list content check paul18fr 6 680 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,155 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Check if two matrix are equal and of not add the matrix to the list quest 3 817 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,511 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 914 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,831 Oct-26-2022, 04:03 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,556 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  check if element is in a list in a dictionary value ambrozote 4 1,962 May-11-2022, 06:05 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,276 Apr-28-2022, 09:32 AM
Last Post: Dexty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020