Open In App

Python | Remove and print every third from list until it becomes empty

Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of numbers, Your task is to remove and print every third number from a list of numbers until the list becomes empty. Examples:

Input : [10, 20, 30, 40, 50, 60, 70, 80, 90] Output : 30 60 90 40 80 50 20 70 10 Explanation: The first third element encountered is 30, after 30 we start the count from 40 for the next third element which is 60, after that 90 is encountered. Then again the count starts from 10 for the next third element which is 40. Proceeding in the same manner as we did before we get next third element after 40 is 80. This process is repeated until the list becomes empty. Input : [1, 2, 3, 4] Output : 3 2 4 1 Explanation: The first third element encountered is 3, after 3 we start the count from 4 for the next third element which is 2. Then again the count starts from 4 for the next third element which is 4 itself and finally the last element 1 is printed.

Approach The index of the list starts from 0 and the first third element will be at position 2. Traverse till the list becomes empty and each time find the index of the next third element and print its corresponding value. After printing reduce the length of the list. 

Python




# Python program to remove to every third
# element until list becomes empty
def removeThirdNumber(int_list):
     
    # list starts with
    # 0 index
    pos = 3 - 1
    index = 0
    len_list = (len(int_list))
     
    # breaks out once the
    # list becomes empty
    while len_list > 0:
     
        index = (pos + index) % len_list
         
        # removes and prints the required
        # element
        print(int_list.pop(index))
        len_list -= 1
 
# Driver code
nums = [1, 2, 3, 4]
removeThirdNumber(nums)


Output:

3
2
4
1

Using generators and yield with a while loop to remove and yield every third element

Algorithm:

Initialize a variable index to 2. This will be used to keep track of the index of the element to be removed.
Enter a while loop that will continue as long as the length of the list lst is greater than 0.
Check if the value of index is greater than or equal to the length of the list lst. If it is, then take the remainder of index divided by the length of the list lst.
Use the pop() method to remove the element at index from the list lst.
Yield the popped element so that it can be returned by the function.
Increment the index by 2 so that the next element to be removed will be two indices away from the previous one.
When the while loop exits, the function will end and return nothing.
 

Python3




def remove_every_third_element(lst):
    # start at index 2, since we want to remove the third element (which has index 2)
    index = 2
    while len(lst) > 0:
        # if we reach the end of the list, loop back to the beginning
        if index >= len(lst):
            index %= len(lst)
        # remove and yield the element at the current index
        yield lst.pop(index)
        # increment the index by 2, since we want to skip the next two elements
        index += 2
 
# example usage
lst = [10, 20, 30, 40, 50, 60, 70, 80, 90]
for i in remove_every_third_element(lst):
    print(i)


Output

30
60
90
40
80
50
20
70
10

In terms of time complexity, the function has a linear time complexity of O(n^2) as it is using pop() method to remove an element from the list.

In terms of auxiliary space, the function has a linear space complexity of O(n) since it is only using one list and no other data structures or variables are being created.



Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads