Check slice with 'in' | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Check slice with 'in'

A slice of a list can be verified with 'in'? In other words; list_a = [1,2,3,4,5,6] [1,2,3] in list_a False But in this way behavior changes, list_a = [[1,2,3],2,3,4,5,6] [1,2,3] in list_a True Is there any way to do first check correctly?

24th Jan 2017, 12:38 PM
Reginaldo Junior
Reginaldo Junior - avatar
2 Answers
+ 2
in the second example you gave you aren't looking for a slice. the whole [1,2,3] is recognized as a unique element. i think you should iterate among all elements of the second list and check them individually against the first one. you can do this with a for loop for each element in the list. if any element is not found the loop is broken and the function returns false.
24th Jan 2017, 1:08 PM
Kleber Leal
Kleber Leal - avatar
24th Jan 2017, 2:20 PM
Kleber Leal
Kleber Leal - avatar