Check if an item is in a nested list

后端 未结 8 1600
死守一世寂寞
死守一世寂寞 2020-11-28 12:32

in a simple list following check is trivial:

x = [1, 2, 3]

2 in x  -> True

but if it is a list of list, such as:

x = [[         


        
相关标签:
8条回答
  • 2020-11-28 13:34

    This would work:

    for arr in x:
        if 2 in arr:
            print True
            break
    

    I would recommend Oscar's answer as any is the right option here.

    0 讨论(0)
  • 2020-11-28 13:37

    try

     2 in [i for sublist in x  for i in sublist]
    
    0 讨论(0)
提交回复
热议问题