- Related Questions & Answers
- Python - Create a dictionary using list with none values
- Find elements of a list by indices in Python
- Python - Ways to find indices of value in list
- Get indices of True values in a binary list in Python
- Find all good indices in the given Array in Python\n
- Find the GCD of N Fibonacci Numbers with given Indices in C++
- Matching odd even indices with values in JavaScript
- Find the Maximum of Similar Indices in two list of Tuples in Python
- Find all triplets in a list with given sum in Python
- Find the sublist with maximum value in given nested list in Python
- Find k longest words in given list in Python
- Find pairs with given product in a sorted Doubly Linked List in Python
- Python Group elements at same indices in a multi-list
- Program to find folded list from a given linked list in Python
- Check for None Tuple in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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.

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)

Running the above code gives us the following result −

Given list : ['Sun', 'Mon', None, 'Wed', None, None] None value positions : [2, 4, 5]

We 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.

listA = ['Sun', 'Mon',None, 'Wed', None, None] # Given list print("Given list : ",listA) # Using enumarate positions = [i for i, val in enumerate(listA) if val == None] # Result print("None value positions : ",positions)

Running the above code gives us the following result −

Given list : ['Sun', 'Mon', None, 'Wed', None, None] None value positions : [2, 4, 5]

Advertisements