0
planets=["Mercury","Venus","Earth","Mars","Jupiter","Saturn","Urenus"]
for planet in planets:
    print(planet)
    del planets[0]
    print(planets)
    planet=planets.pop()
    print(f"{planet} and {planets} and {len(planets)}")

1

David Buck
  • 3,752
  • 35
  • 31
  • 35

1 Answers1

1

I'm not sure the purpose of this code but for the list to be empty, how about:

planets=["Mercury","Venus","Earth","Mars","Jupiter","Saturn","Urenus"]
while len(planets):
    print(planet)
    del planets[0]
    print(planets)
    if len(planets):
        planet=planets.pop()
        print(f"{planet} and {planets} and {len(planets)}")

which yields:

Jupiter
['Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Urenus']
Urenus and ['Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn'] and 5
Urenus
['Earth', 'Mars', 'Jupiter', 'Saturn']
Saturn and ['Earth', 'Mars', 'Jupiter'] and 3
Saturn
['Mars', 'Jupiter']
Jupiter and ['Mars'] and 1
Jupiter
[]

Kr.

antoine
  • 662
  • 4
  • 10
  • I apologize if it is very absurd program in terms of programming, I am just a beginner finding out how things work in programming. And Thank you, sir for your interest to answer my question, so May I ask you, if it is not possible in for loop? – Ravi Shankar Jan 13 '21 at 15:01