{"id":20660,"date":"2021-09-21T08:59:39","date_gmt":"2021-09-21T03:29:39","guid":{"rendered":"https:\/\/python-programs.com\/?p=20660"},"modified":"2021-11-22T18:36:17","modified_gmt":"2021-11-22T13:06:17","slug":"python-program-to-remove-elements-from-the-array-list-which-occurs-more-than-k-times","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-remove-elements-from-the-array-list-which-occurs-more-than-k-times\/","title":{"rendered":"Python Program to Remove Elements from the Array\/List Which Occurs More than k Times"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Delete All Odd Frequency Elements from an Array\/List<\/a><\/p>\n Given a list, k value and the task is to delete all the elements from a given list that appears more than k times in python.<\/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 delete all the elements from a given list that appear more than k times in python:<\/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 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 Enhance your coding skills with our list of Python Basic Programs<\/a> provided and become a pro in the general-purpose programming language Python in no time.<\/p>\n In the previous article, we have discussed Python Program to Delete All Odd Frequency Elements from an Array\/List Given a list, k value and the task is to delete all the elements from a given list that appears more than k times in python. Examples: Example1: Input: Given List = [4, 5, 8, 9, 4, …<\/p>\nGiven List = [4, 5, 8, 9, 4, 6, 3, 3, 3]\r\nGiven k value = 2<\/pre>\n
The list [4, 5, 8, 9, 4, 6, 3, 3, 3] after deletion all the elements from a given list that appears more than k times:\r\n[4, 5, 8, 9, 4, 6]<\/pre>\n
Given List = [5, 6, 8, 1, 5, 6, 1, 1, 1]\r\nGiven k value = 3<\/pre>\n
The list [5, 6, 8, 1, 5, 6, 1, 1, 1] after deletion all the elements from a given list that appears more than k times:\r\n[5, 6, 8, 5, 6]<\/pre>\n
Program to Remove Elements from the Array\/List Which Occurs More than k Times in Python<\/h2>\n
\n
Method #1: Using Dictionary (Hashing, Static Input)<\/h3>\n
\n
# Take a dictionary and initialize it to empty\r\n# using the {} or dict() say freqncyDictionary.\r\nfreqncyDictionary = {}\r\n# Give the list as static input and store it in a variable.\r\ngvnlst = [4, 5, 8, 9, 4, 6, 3, 3, 3]\r\n# Loop in the given list using the For loop.\r\nfor i in gvnlst:\r\n # Inside the For loop,\r\n # Check if the list element is present in the dictionary\r\n # or not using the if conditional statement and 'in' keyword.\r\n if i in freqncyDictionary.keys():\r\n # If it is true then increment the count of the list element\r\n # in the dictionary by 1.\r\n freqncyDictionary[i] = freqncyDictionary[i]+1\r\n # Else initialize the dictionary with the list element as key and value as 1.\r\n else:\r\n freqncyDictionary[i] = 1\r\n# Give the k value as static input and store it in a variable.\r\nk = 2\r\n# Take a new empty list say modifdlst and store it in a variable.\r\nmodifdlst = []\r\n# Loop in the given list using the For loop.\r\nfor lstelmt in gvnlst:\r\n # Check if the key in the freqncyDictionary having value less than or equal to k\r\n # using the if conditional statement.\r\n\r\n if(freqncyDictionary[lstelmt] <= k):\r\n # If it is true then append the key value to the above declared empty list modifdlst.\r\n modifdlst.append(lstelmt)\r\n# Print the list after deletion of all the elements from a given list that appears\r\n# more than k times\r\nprint(\"The list\", gvnlst,\r\n \"after deletion all the elements from a given list that appears more than k times:\")\r\nprint(modifdlst)\r\n<\/pre>\n
The list [4, 5, 8, 9, 4, 6, 3, 3, 3] after deletion all the elements from a given list that appears more than k times:\r\n[4, 5, 8, 9, 4, 6]<\/pre>\n<\/div>\n
Method #2: Using Dictionary (Hashing, User Input)<\/h3>\n
\n
# Take a dictionary and initialize it to empty\r\n# using the {} or dict() say freqncyDictionary.\r\nfreqncyDictionary = {}\r\n# Give the list as user input using the list(),map(),split(),int functions\r\n# and store it in a variable.\r\ngvnlst = list(\r\n map(int, input('Enter some random list element separated by spaces = ').split()))\r\n# Loop in the given list using the For loop.\r\nfor i in gvnlst:\r\n # Inside the For loop,\r\n # Check if the list element is present in the dictionary\r\n # or not using the if conditional statement and 'in' keyword.\r\n if i in freqncyDictionary.keys():\r\n # If it is true then increment the count of the list element\r\n # in the dictionary by 1.\r\n freqncyDictionary[i] = freqncyDictionary[i]+1\r\n # Else initialize the dictionary with the list element as key and value as 1.\r\n else:\r\n freqncyDictionary[i] = 1\r\n# Give the k value as user input using the int(input()) function and store it in a variable.\r\nk = int(input(\"Enter some random number =\"))\r\n# Take a new empty list say modifdlst and store it in a variable.\r\nmodifdlst = []\r\n# Loop in the given list using the For loop.\r\nfor lstelmt in gvnlst:\r\n # Check if the key in the freqncyDictionary having value less than or equal to k\r\n # using the if conditional statement.\r\n\r\n if(freqncyDictionary[lstelmt] <= k):\r\n # If it is true then append the key value to the above declared empty list modifdlst.\r\n modifdlst.append(lstelmt)\r\n# Print the list after deletion of all the elements from a given list that appears\r\n# more than k times\r\nprint(\"The list\", gvnlst,\r\n \"after deletion all the elements from a given list that appears more than k times:\")\r\nprint(modifdlst)\r\n<\/pre>\n
Enter some random list element separated by spaces = 3 1 5 6 1 2 2\r\nEnter some random number =1\r\nThe list [3, 1, 5, 6, 1, 2, 2] after deletion all the elements from a given list that appears more than k times:\r\n[3, 5, 6]<\/pre>\n
Method #3: Using Counter() function (Hashing, Static Input)<\/h3>\n
\n
# Import the Counter() function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the list as static input and store it in a variable.\r\ngvnlst = [5, 6, 8, 1, 5, 6, 1, 1, 1]\r\n# Calculate the frequency of all the given list elements using the Counter()\r\n# function which returns the element and its frequency as key-value pair\r\n# and store this dictionary in a variable(say freqncyDictionary)\r\nfreqncyDictionary = Counter(gvnlst)\r\n# Give the k value as static input and store it in a variable.\r\nk = 3\r\n# Take a new empty list say modifdlst and store it in a variable.\r\nmodifdlst = []\r\n# Loop in the given list using the For loop.\r\nfor lstelmt in gvnlst:\r\n # Check if the key in the freqncyDictionary having value less than or equal to k\r\n # using the if conditional statement.\r\n\r\n if(freqncyDictionary[lstelmt] <= k):\r\n # If it is true then append the key value to the above declared empty list modifdlst.\r\n modifdlst.append(lstelmt)\r\n# Print the list after deletion of all the elements from a given list that appears\r\n# more than k times\r\nprint(\"The list\", gvnlst,\r\n \"after deletion all the elements from a given list that appears more than k times:\")\r\nprint(modifdlst)\r\n<\/pre>\n
The list [5, 6, 8, 1, 5, 6, 1, 1, 1] after deletion all the elements from a given list that appears more than k times:\r\n[5, 6, 8, 5, 6]<\/pre>\n
Method #4: Using Counter() function (Hashing, User Input)<\/h3>\n
\n
# Import the Counter() function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the list as user input using the list(),map(),split(),int functions\r\n# and store it in a variable.\r\ngvnlst = list(\r\n map(int, input('Enter some random list element separated by spaces = ').split()))\r\n# Calculate the frequency of all the given list elements using the Counter()\r\n# function which returns the element and its frequency as key-value pair\r\n# and store this dictionary in a variable(say freqncyDictionary)\r\nfreqncyDictionary = Counter(gvnlst)\r\n# Give the k value as user input using the int(input()) function and store it in a variable.\r\nk = int(input(\"Enter some random number =\"))\r\n# Take a new empty list say modifdlst and store it in a variable.\r\nmodifdlst = []\r\n# Loop in the given list using the For loop.\r\nfor lstelmt in gvnlst:\r\n # Check if the key in the freqncyDictionary having value less than or equal to k\r\n # using the if conditional statement.\r\n\r\n if(freqncyDictionary[lstelmt] <= k):\r\n # If it is true then append the key value to the above declared empty list modifdlst.\r\n modifdlst.append(lstelmt)\r\n# Print the list after deletion of all the elements from a given list that appears\r\n# more than k times\r\nprint(\"The list\", gvnlst,\r\n \"after deletion all the elements from a given list that appears more than k times:\")\r\nprint(modifdlst)\r\n<\/pre>\n
Enter some random list element separated by spaces = 3 1 5 6 1 2 2\r\nEnter some random number =1\r\nThe list [3, 1, 5, 6, 1, 2, 2] after deletion all the elements from a given list that appears more than k times:\r\n[3, 5, 6]<\/pre>\n
\n