
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Indices of Non-None Elements in Python
The problem at hand is to get the indices of non-none elements in the given input list and implement the solution using Python. Sometimes we need to find the empty of none values in the given dataset so this problem will be helpful to solve these kinds of problems.
Understanding the Problem
In the given problem we have to find the non-none item's indices in the given input lists. This problem can be helpful to filter out none values from the list or whenever we want to perform operations on none items. For example we will be given a list like [1, None, 5, None, 8, 9], in this list after finding the non-None items in the list we will return its indices in the new list as [0, 2, 4, 5].
Logic for The Above Problem
To solve the given problem and find the indices of non-None items in the given list or array, we will traverse over the elements one by one and also check that the value is not equal to None. If the None value is found then we will save its index in the separate array. After completing the iteration we will have the indices of non-None items.
Algorithm
Step 1 First step is to define the function called get_index(). Inside this function we will pass items list as an input.
Step 2 Then initiate a blank list or array as index = []. This list will be used to store the indices of non-None items.
Step 3 Next, a loop is initiated over the values of the items of the given input list. Inside the loop we will check the condition, if the value is not equal to None.
Step 4 If the iterated item is not None, append the index value in the list called index we have created in step 2.
Step 5 At the end we will return the index list which is containing the indices of non-None items.
Example
# Define the function to get the non-none items index def get_index(items): index = [] for i in range(len(items)): if items[i] is not None: index.append(i) return index # Initialize the list with having None items the_list = [12, None, 54, None, 31, None, 42, None, 83] # Call the function non_none_indices = get_index(the_list) print("The indices of non-None elements:\n", non_none_indices)
Output
The indices of non-None elements: [0, 2, 4, 6, 8]
Complexity
The time complexity for finding the indices of non-None items in the list is O(N), here N is the size of the given input the_list. As we have traversed the given list exactly once for performing the constant time operation. The space complexity for this code is O(K), K is the number of non-None items in the ?index' list.
Conclusion
The code is successfully showing the indices of the non-None item in the_list. This is a common problem while working with lists or arrays in Python. So we have learned how we can find out the indices of non-None items in this article..