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
#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


Messages In This Thread
RE: How to check if a list is in another list - by bowlofred - Jan-17-2022, 05:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 2 242 May-01-2024, 07:16 AM
Last Post: Gribouillis
  Help with to check an Input list data with a data read from an external source sacharyya 3 462 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  [solved] list content check paul18fr 6 766 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,242 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 878 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,594 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 947 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,891 Oct-26-2022, 04:03 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,617 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  check if element is in a list in a dictionary value ambrozote 4 2,021 May-11-2022, 06:05 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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