Change language

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

|

Given a list of numbers, your task is to remove and print every third number from the 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 at 0 and the first third element will be at position 2. Go through until the list is 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.

Output:

 3 2 4 1 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically

# Python program to remove every third
# element, until the list is empty

def removeThirdNumber (int_list):

 

# the list starts with

# 0 index

pos = 3 - 1

index = 0

  len_list = ( len (int_list))

 

# flashes as soon as

# list becomes empty

while len_list"  0

 

index = (pos + index) % len_list

 

# removes and prints required

# element

print (int_list.pop ( index)) 

len_list - = 1

 
# Driver code

nums = [ 1 , 2 , 3 , 4

removeThirdNumber (nums)