Python list with None
Ngày đăng:
07/01/2022
Trả lời:
0
Lượt xem:
125
Find indices with None values in given list in PythonPythonServer Side ProgrammingProgramming Manytime when dealing with data analysis we may come across the None values present in a list. These values can not be used directly in mathematical operations and string operations etc. So we need to find their position and either convert them or use them effectively. With range()Combining the range and len function we can compare the value of each element with None and capture their index positions. Of course we use a for loop design to achieve this. ExampleLive Demo listA = ['Sun', 'Mon',None, 'Wed', None, None] # Given list print("Given list : ",listA) # Using range positions = [i for i in range(len(listA)) if listA[i] == None] # Result print("None value positions : ",positions)OutputRunning the above code gives us the following result With enumerateWe can also use the enumerate function which lists out ech element. Then we compare each element with the None value and select its position as shown below in the program. ExampleLive Demo OutputRunning the above code gives us the following result Given list : ['Sun', 'Mon', None, 'Wed', None, None] None value positions : [2, 4, 5]Pradeep Elance Published on 26-Aug-2020 07:49:55 Previous Page Print Page Next Page Advertisements |