Python program to Remove and print every third from list until it becomes empty?


In this article, we will learn how to remove and print every third element or item from the list until it becomes empty using Python Program.

First we create a list, the index of the starting address is 0 and the position of the first third element is 2 and need to traverse till the list becomes empty and another important work to do every time we have to find the index of the next third element and print the value and after that reduce the length of the list.

Input-Output Scenario

Following is the input and its output scenario for removing and printing every third element from the list until it becomes empty −

Input: [15,25,35,45,55,65,75,85,95] 
Output : 35,65,95,45,85,55,25,75,15

Here, the first third element is 35, following which we begin counting from 44 for the second third element, which is 65, and so on until 95 is reached. The count again begins at 15 for the subsequent third element, which is 45. By continuing in the same way as before, we arrive to the third element following 45, which is 85. Up until the list is completely empty, this process is repeated.

Algorithm

Following is an algorithm or approach of how to remove and print every third element or item from the list until it becomes empty −

  • The index of the list starts from 0 and the first third element will be at position 2.

  • Find the length of the list.

  • Traverse till the list becomes empty and each time find the index of the next third element.

  • End of the program.

Example

with user input

Following is an illustration of the above explained steps −

# To remove to every third element until list becomes empty def removenumber(no): # list starts with # 0 index p = 3 - 1 id = 0 lenoflist = (len(no)) # breaks out once the # list becomes empty while lenoflist > 0: id = (p + id) % lenoflist # removes and prints the required # element print(no.pop(id)) lenoflist -= 1 # Driver code A=list() n=int(input("Enter the size of the array ::")) print("Enter the INTEGER number") for i in range(int(n)): p=int(input("n=")) A.append(int(p)) print("After remove third element, The List is") # call function removenumber(A)

Output

Following is an output of the above code −

Enter the size of the array ::9
Enter the number
n=10
n=20
n=30
n=40
n=50
n=60
n=70
n=80
n=90
After remove third element, The List is
30
60
90
40
80
50
20
70
10

Example

with static input

Following is an example for removing and printing every third element from the list until it becomes empty by providing static input −

# To remove to every third element until list becomes empty def removenumber(no): # list starts with # 0 index p = 3 - 1 id = 0 lenoflist = (len(no)) # breaks out once the # list becomes empty while lenoflist > 0: id = (p + id) % lenoflist # removes and prints the required # element print(no.pop(id)) lenoflist -= 1 # Driver code elements = [15,25,35,45,55,65,75,85,95] # call function removenumber(elements)

Output

Following is an output of the above code −

35
65
95
45
85
55
25
75
15

Updated on: 23-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements