Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 from a list until it becomes empty using Python. This classic problem demonstrates circular traversal and dynamic list manipulation.
Problem Understanding
We need to repeatedly find and remove every third element from a list, starting from index 2 (third position). After removing an element, we continue counting from the next position in the modified list until all elements are removed.
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
The process works as follows: start at index 2 (element 35), remove it. Continue counting 3 positions from the current position in the shortened list to find the next element to remove, and repeat until the list is empty.
Algorithm
Following is the algorithm to remove and print every third element until the list becomes empty ?
− Initialize position counter to 2 (third element index)
− While the list is not empty, calculate the next third element using modulo operation
− Remove and print the element at the calculated position
− Update the list length and continue until empty
Using Static Input
Here's how to implement the solution with predefined input ?
def remove_every_third(numbers):
# Starting position for third element (0-indexed)
step = 3 - 1 # Convert to 0-based indexing
current_index = 0
list_length = len(numbers)
# Continue until list becomes empty
while list_length > 0:
# Calculate next third element position using modulo
current_index = (step + current_index) % list_length
# Remove and print the element
print(numbers.pop(current_index))
list_length -= 1
# Test with static input
elements = [15, 25, 35, 45, 55, 65, 75, 85, 95]
print("Original list:", elements.copy())
print("Removed elements in order:")
remove_every_third(elements)
Original list: [15, 25, 35, 45, 55, 65, 75, 85, 95] Removed elements in order: 35 65 95 45 85 55 25 75 15
Using User Input
Here's the implementation that accepts user input for the list elements ?
def remove_every_third(numbers):
step = 3 - 1
current_index = 0
list_length = len(numbers)
while list_length > 0:
current_index = (step + current_index) % list_length
print(numbers.pop(current_index))
list_length -= 1
# Get input from user
user_list = []
n = int(input("Enter the size of the array: "))
print("Enter the numbers:")
for i in range(n):
element = int(input(f"Element {i+1}: "))
user_list.append(element)
print("Removed elements in order:")
remove_every_third(user_list)
Enter the size of the array: 9 Enter the numbers: Element 1: 10 Element 2: 20 Element 3: 30 Element 4: 40 Element 5: 50 Element 6: 60 Element 7: 70 Element 8: 80 Element 9: 90 Removed elements in order: 30 60 90 40 80 50 20 70 10
How It Works
The algorithm uses the modulo operator to handle circular traversal. When we reach the end of the list, the modulo operation wraps us back to the beginning. The key insight is that after removing an element, the list shrinks, so we recalculate positions based on the new length.
Conclusion
This problem demonstrates the Josephus problem variant where every third element is eliminated. The modulo operation ensures proper circular traversal, and the pop() method efficiently removes elements while returning their values.
