{"id":14604,"date":"2021-09-30T16:30:16","date_gmt":"2021-09-30T11:00:16","guid":{"rendered":"https:\/\/python-programs.com\/?p=14604"},"modified":"2021-11-22T18:34:24","modified_gmt":"2021-11-22T13:04:24","slug":"python-program-to-find-indices-of-the-non-zero-elements-in-the-python-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-indices-of-the-non-zero-elements-in-the-python-list\/","title":{"rendered":"Python Program to Find Indices of the Non-Zero Elements in the Python list"},"content":{"rendered":"
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.<\/p>\n
Lists:<\/strong><\/p>\n 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.<\/p>\n 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.<\/p>\n Given a list, the task is to find the indices of non-zero elements in the given list.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the indices of non-zero elements in the given list.<\/p>\n Explore complete java concepts from the Java programming examples<\/a> and get ready to become a good programmer and crack the java software developer interview with ease.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach: Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\nGiven list = [11, 19, 0, 8, 45, 0, 29, 0, 19, 0, 26, 0, 33]<\/pre>\n
The non zero elements indices in the given list are = [0, 1, 3, 4, 6, 8, 10, 12]<\/pre>\n
Given list = 25 0 7 0 0 0 9 5<\/pre>\n
The non zero elements indices in the given list are = [0, 2, 6, 7]<\/pre>\n
Program to Find Indices of the Non-Zero Elements in the Python list in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvnlst = [11, 19, 0, 8, 45, 0, 29, 0, 19, 0, 26, 0, 33]\r\n# Take an empty list and initialize it\u2019s with an empty list using list() and [].\r\nnwlist = []\r\n# Calculate the length of the given list using the len() function.\r\ngvnlstlen = len(gvnlst)\r\n# Loop till the length of the given list using the For loop.\r\nfor m in range(gvnlstlen):\r\n # Check if the element in the given list is equal to 0 or not\r\n # using the If conditional statement.\r\n if(gvnlst[m] != 0):\r\n # If it is non-zero element then append the index to the new list\r\n # using the append() function.\r\n nwlist.append(m)\r\n# Print the index list.\r\nprint('The non zero elements indices in the given list are = ', nwlist)\r\n<\/pre>\n
The non zero elements indices in the given list are = [0, 1, 3, 4, 6, 8, 10, 12]<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n<\/strong><\/p>\n\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvnlst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Take an empty list and initialize it\u2019s with an empty list using list() and [].\r\nnwlist = []\r\n# Calculate the length of the given list using the len() function.\r\ngvnlstlen = len(gvnlst)\r\n# Loop till the length of the given list using the For loop.\r\nfor m in range(gvnlstlen):\r\n # Check if the element in the given list is equal to 0 or not\r\n # using the If conditional statement.\r\n if(gvnlst[m] != 0):\r\n # If it is non-zero element then append the index to the new list\r\n # using the append() function.\r\n nwlist.append(m)\r\n# Print the index list.\r\nprint('The non zero elements indices in the given list are = ', nwlist)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 25 0 7 0 0 0 9 5\r\nThe non zero elements indices in the given list are = [0, 2, 6, 7]<\/pre>\n
Method #3: Using List Comprehension (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvnlst = [11, 19, 0, 8, 45, 0, 29, 0, 19, 0, 26, 0, 33]\r\n# Use the list comprehension and enumerate to find all non-zero elements\r\n# indices in the given list.\r\nnwlist = [index for index, listelement in enumerate(\r\n gvnlst) if listelement != 0]\r\n# Print the index list.\r\nprint('The non zero elements indices in the given list are = ', nwlist)\r\n<\/pre>\n
The non zero elements indices in the given list are = [0, 1, 3, 4, 6, 8, 10, 12]<\/pre>\n
Method #4: Using List Comprehension (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvnlst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Use the list comprehension and enumerate to find all non-zero elements\r\n# indices in the given list.\r\nnwlist = [index for index, listelement in enumerate(\r\n gvnlst) if listelement != 0]\r\n# Print the index list.\r\nprint('The non zero elements indices in the given list are = ', nwlist)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 7 9 0 0 6 3 0 0\r\nThe non zero elements indices in the given list are = [0, 1, 4, 5]<\/pre>\n
\n