Program to Find Indices of the Non-Zero Elements in the Python list

Python Program to Find Indices of the Non-Zero Elements in the Python list

We’ll look at how to determine the indices of non-zero entries in a Python list. There may be times when we simply need to access the list’s non-zero elements. In such instances, we can apply the following techniques.

Lists:

A list is an ordered grouping of values. It can hold a variety of values. A list is a container that may be changed. This means that we can add new values, delete old ones, or change current ones.

A Python list is a mathematical concept that describes a finite sequence. Items or elements in a list are the values in a list. A list can have multiple instances of the same value. Each occurrence is treated as a separate item.

Given a list, the task is to find the indices of non-zero elements in the given list.

Examples:

Example1:

Input:

Given list = [11, 19, 0, 8, 45, 0, 29, 0, 19, 0, 26, 0, 33]

Output:

The non zero elements indices in the given list are =  [0, 1, 3, 4, 6, 8, 10, 12]

Example2:

Input:

Given list = 25 0 7 0 0 0 9 5

Output:

The non zero elements indices in the given list are = [0, 2, 6, 7]

Program to Find Indices of the Non-Zero Elements in the Python list in Python

Below are the ways to find the indices of non-zero elements in the given list.

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Take an empty list and initialize it’s with an empty list using list() and [].
  • Calculate the length of the given list using the len() function.
  • Loop till the length of the given list using the For loop.
  • Check if the element in the given list is equal to 0 or not using the If conditional statement.
  • If it is a non-zero element then append the index to the new list using the append() function.
  • Print the index list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvnlst = [11, 19, 0, 8, 45, 0, 29, 0, 19, 0, 26, 0, 33]
# Take an empty list and initialize it’s with an empty list using list() and [].
nwlist = []
# Calculate the length of the given list using the len() function.
gvnlstlen = len(gvnlst)
# Loop till the length of the given list using the For loop.
for m in range(gvnlstlen):
    # Check if the element in the given list is equal to 0 or not
    # using the If conditional statement.
    if(gvnlst[m] != 0):
        # If it is non-zero element then append the index to the new list
        # using the append() function.
        nwlist.append(m)
# Print the index list.
print('The non zero elements indices in the given list are = ', nwlist)

Output:

The non zero elements indices in the given list are =  [0, 1, 3, 4, 6, 8, 10, 12]

Method #2: Using For Loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Take an empty list and initialize it’s with an empty list using list() and [].
  • Calculate the length of the given list using the len() function.
  • Loop till the length of the given list using the For loop.
  • Check if the element in the given list is equal to 0 or not using the If conditional statement.
  • If it is a non-zero element then append the index to the new list using the append() function.
  • Print the index list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvnlst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Take an empty list and initialize it’s with an empty list using list() and [].
nwlist = []
# Calculate the length of the given list using the len() function.
gvnlstlen = len(gvnlst)
# Loop till the length of the given list using the For loop.
for m in range(gvnlstlen):
    # Check if the element in the given list is equal to 0 or not
    # using the If conditional statement.
    if(gvnlst[m] != 0):
        # If it is non-zero element then append the index to the new list
        # using the append() function.
        nwlist.append(m)
# Print the index list.
print('The non zero elements indices in the given list are = ', nwlist)

Output:

Enter some random List Elements separated by spaces = 25 0 7 0 0 0 9 5
The non zero elements indices in the given list are = [0, 2, 6, 7]

Method #3: Using List Comprehension (Static Input)

The method used in the sample program is another way to find the indices of non-zero elements. This is a concise summary implementation of the preceding algorithm. To transform the list into an iterable, we use the enumerate() method in this method.

Approach:

  • Give the list as static input and store it in a variable.
  • Use the list comprehension and enumerate to find all non-zero elements indices in the given list.
  • Print this indices list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvnlst = [11, 19, 0, 8, 45, 0, 29, 0, 19, 0, 26, 0, 33]
# Use the list comprehension and enumerate to find all non-zero elements
# indices in the given list.
nwlist = [index for index, listelement in enumerate(
    gvnlst) if listelement != 0]
# Print the index list.
print('The non zero elements indices in the given list are = ', nwlist)

Output:

The non zero elements indices in the given list are =  [0, 1, 3, 4, 6, 8, 10, 12]

Method #4: Using List Comprehension (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Use the list comprehension and enumerate to find all non-zero elements indices in the given list.
  • Print this indices list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvnlst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Use the list comprehension and enumerate to find all non-zero elements
# indices in the given list.
nwlist = [index for index, listelement in enumerate(
    gvnlst) if listelement != 0]
# Print the index list.
print('The non zero elements indices in the given list are = ', nwlist)

Output:

Enter some random List Elements separated by spaces = 7 9 0 0 6 3 0 0
The non zero elements indices in the given list are = [0, 1, 4, 5]

Related Programs: