python if in composite list

Solutions on MaxInterview for python if in composite list by the best coders in the world

showing results for - "python if in composite list"
Andrea
23 May 2018
1# Python code to demonstrate 
2# finding whether element 
3# exists in listof list 
4  
5# initialising nested lists 
6ini_list = [[1, 2, 5, 10, 7], 
7            [4, 3, 4, 3, 21], 
8            [45, 65, 8, 8, 9, 9]] 
9  
10elem_to_find = 8
11elem_to_find1 = 0
12  
13# element exists in listof listor not? 
14res1 = any(elem_to_find in sublist for sublist in ini_list) 
15res2 = any(elem_to_find1 in sublist for sublist in ini_list) 
16  
17# printing result 
18print(str(res1), "\n", str(res2)) 
19
20#Output:
21#True
22#False