How to check if an element of a list is a list (in Python)?

前端 未结 5 979
别跟我提以往
别跟我提以往 2020-12-25 09:41

If we have the following list:

list = [\'UMM\', \'Uma\', [\'Ulaster\',\'Ulter\']]

If I need to find out if an element in the list is itself

5条回答
  •  心在旅途
    2020-12-25 09:51

    Expression you are looking for may be:

    ...
    return any( isinstance(e, list) for e in my_list )
    

    Testing:

    >>> my_list = [1,2]
    >>> any( isinstance(e, list) for e in my_list )
    False
    >>> my_list = [1,2, [3,4,5]]
    >>> any( isinstance(e, list) for e in my_list )
    True
    >>> 
    

提交回复
热议问题