Python | Find indices with None values in given list
Many times while working in data science domain we need to get the list of all the indices which are None, so that they can be easily be prepossessed. This is quite a popular problem and solution to it comes quite handy. Let’s discuss certain ways in which this can be done.
Method #1 : Using list comprehension + range() In this method we just check for each index using the range function and store the index if we find that index is None.
Python3
# Python3 code to demonstrate # finding None indices in list # using list comprehension + enumerate # initializing list test_list = [ 1 , None , 4 , None , None , 5 ] # printing original list print ("The original list : " + str (test_list)) # using list comprehension + enumerate # finding None indices in list res = [i for i in range ( len (test_list)) if test_list[i] = = None ] # print result print ("The None indices list is : " + str (res)) |
The original list : [1, None, 4, None, None, 5] The None indices list is : [1, 3, 4]
Method #2 : Using list comprehension + enumerate() The enumerate function can be used to iterate together the key and value pair and list comprehension can be used to bind all this logic in one line.
Python3
# Python3 code to demonstrate # finding None indices in list # using list comprehension + enumerate # initializing list test_list = [ 1 , None , 4 , None , None , 5 ] # printing original list print ("The original list : " + str (test_list)) # using list comprehension + enumerate # finding None indices in list res = [i for i, val in enumerate (test_list) if val = = None ] # print result print ("The None indices list is : " + str (res)) |
The original list : [1, None, 4, None, None, 5] The None indices list is : [1, 3, 4]
Method #3 : Using filter() and lambda
Alternatively, you can use the filter function and a lambda function to achieve the same result:
Python3
test_list = [ 1 , None , 4 , None , None , 5 ] # Use the filter function and a lambda function to get a generator # that yields tuples of the form (index, element) for which the element # is None. indices = filter ( lambda i_x: i_x[ 1 ] is None , enumerate (test_list)) # Use a list comprehension to extract the indices from the tuples # returned by the generator. res = [i for i, x in indices] print ( "The None indices list is : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
The None indices list is : [1, 3, 4]
This code first uses the enumerate function to generate tuples of the form (index, element) for each element in test_list. It then uses the filter function and a lambda function to filter out the tuples for which the element is not None. The lambda function receives a tuple i_x and returns True if the second element of the tuple (i.e. i_x[1]) is None, and False otherwise. The filter function returns a generator that yields the tuples for which the lambda function returned True.
Finally, the code uses a list comprehension to extract the indices from the tuples returned by the generator and stores them in the result list.
In terms of time complexity, this code has a complexity of O(n) since it needs to iterate through the list once to generate the tuples and filter them. In terms of space complexity, it has a complexity of O(n).
Please Login to comment...