{"id":18196,"date":"2021-08-26T10:42:21","date_gmt":"2021-08-26T05:12:21","guid":{"rendered":"https:\/\/python-programs.com\/?p=18196"},"modified":"2021-11-22T18:37:13","modified_gmt":"2021-11-22T13:07:13","slug":"python-program-to-find-leaders-in-an-array-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-leaders-in-an-array-list\/","title":{"rendered":"Python Program to Find Leaders in an Array\/List"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Check Automorphic Number or Not<\/a> If an element is greater than all of the elements on its right side, it is the leader. And the last element is always a leader.<\/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 Given a list, the task is to find all the leaders of the given list in python.<\/p>\n Below are the ways to find all the leaders of the given list in python some of them are:<\/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 Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Check Automorphic Number or Not Leader: If an element is greater than all of the elements on its right side, it is the leader. And the last element is always a leader. Examples: Example1: Input: Given list =[23, 11, 1, 7, 8, 6, 3] Output: …<\/p>\n
\nLeader:<\/strong><\/p>\nGiven list =[23, 11, 1, 7, 8, 6, 3]<\/pre>\n
The leaders of the given list [23, 11, 1, 7, 8, 6, 3] are :\r\n23\r\n11\r\n8\r\n6\r\n3<\/pre>\n
Given List = [1, 2, 3, 7, 8, 6]<\/pre>\n
The leaders of the given list [1, 2, 3, 7, 8, 6] are :\r\n8\r\n6<\/pre>\n
Program to Find Leaders in an Array\/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\ngvnlstt = [23, 11, 1, 7, 8, 6, 3]\r\n# Calculate the length of the list and store it in another variable.\r\nlstleng = len(gvnlstt)\r\nprint('The leaders of the given list', gvnlstt, 'are :')\r\n# Loop from 0 to the length of the list using the For loop.\r\nfor m in range(lstleng):\r\n # Loop from parent loop iterator value to the length of the list\r\n # using another Nested for loop(Inner For loop)\r\n for n in range(m+1, lstleng):\r\n # Check if the element at the index of the parent loop iterator value\r\n # is less than or equal to the element at the index of the inner loop iterator value.\r\n # (gvnlist[m] <= gvnlist[n]) using the if conditional statement.\r\n if (gvnlstt[m] <= gvnlstt[n]):\r\n # If it is true then break the inner loop using the break keyword.\r\n break\r\n\r\n # After the end of the inner For loop check if the inner loop iterator value\r\n # is equal to the length of the given list - 1 using the if conditional statement.\r\n if(n == lstleng-1):\r\n # If it is true then it is the leader so print it.\r\n print(gvnlstt[m])\r\n<\/pre>\n
The leaders of the given list [23, 11, 1, 7, 8, 6, 3] are :\r\n23\r\n11\r\n8\r\n6\r\n3<\/pre>\n
Method #2: Using For loop (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\ngvnlstt = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Calculate the length of the list and store it in another variable.\r\nlstleng = len(gvnlstt)\r\nprint('The leaders of the given list', gvnlstt, 'are :')\r\n# Loop from 0 to the length of the list using the For loop.\r\nfor m in range(lstleng):\r\n # Loop from parent loop iterator value to the length of the list\r\n # using another Nested for loop(Inner For loop)\r\n for n in range(m+1, lstleng):\r\n # Check if the element at the index of the parent loop iterator value\r\n # is less than or equal to the element at the index of the inner loop iterator value.\r\n # (gvnlist[m] <= gvnlist[n]) using the if conditional statement.\r\n if (gvnlstt[m] <= gvnlstt[n]):\r\n # If it is true then break the inner loop using the break keyword.\r\n break\r\n\r\n # After the end of the inner For loop check if the inner loop iterator value\r\n # is equal to the length of the given list - 1 using the if conditional statement.\r\n if(n == lstleng-1):\r\n # If it is true then it is the leader so print it.\r\n print(gvnlstt[m])\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 1 2 3 7 8 6\r\nThe leaders of the given list [1, 2, 3, 7, 8, 6] are :\r\n8\r\n6<\/pre>\n
\n