100 days of code: Day 6 - dynamic lists

Last modified on 2020-06-14

Source: 100-days-of-code.

A more dynamic version (changing, adding, and removing elements in a list) of yesterday’s program - rockets2.py - that outputs my wish to ride on a rocket …

rockets = []
rockets_tbd = []
ride = "\nI would like to go for a ride on a "

# add new elements to list
rockets.append('v-2')
rockets.append('soyuz')
rockets.append('long march')
rockets.append('falcon 9')
print('Prospective rocket list: ' + str(rockets))
# find the length of a list
print("\nMy rocket wishlist has " + str(len(rockets)) 
        + " rockets to start with.")
print(ride + f"{rockets[0].title()} rocket.")
print("On second thought, maybe not. Not really astronaut-friendly vehicles.")
# remove a list item when its position is known
del rockets[0]
# add a new element at specified position in the list
rockets.insert(0, 'saturn v')
print(ride + f"{rockets[0].title()} rocket.")
print("Will need a bit of restoration work, though.")
rockets_tbd.append(rockets[0])
# remove the first occurance of a list item by value
rockets.remove('saturn v')
print(ride + f"{rockets[0].title()} rocket.")
print("Tickets are expensive!")
rockets_tbd.append(rockets[0])
# 'pop' (remove) a list item and use in a string
backup_rocket = rockets.pop(0)
print(f"{backup_rocket.title()} can be a backup option.")
print(ride + f"{rockets[0].title()} rocket.")
print("Only taikonauts are allowed to ride (so far).")
print(ride + f"{rockets[1].title()} rocket.")
print("Just flew their first two paying customers!")
# modify an element in a list
rockets[-1] = 'starship'
print("\nHowever I am _especially_ looking forward to a ride on the "
            + f"upcoming {rockets[-1].title()} rocket.")
print(f"{rockets[-1].title()} it is! With {rockets_tbd[-1].title()} and "
        + f"{rockets_tbd[0].title()} as backup options.")

… outputs …

$ python3 rockets2.py 
Prospective rocket list: ['v-2', 'soyuz', 'long march', 'falcon 9']

My rocket wishlist has 4 rockets to start with.

I would like to go for a ride on a V-2 rocket.
On second thought, maybe not. Not really astronaut-friendly vehicles.

I would like to go for a ride on a Saturn V rocket.
Will need a bit of restoration work, though.

I would like to go for a ride on a Soyuz rocket.
Tickets are expensive!
Soyuz can be a backup option.

I would like to go for a ride on a Long March rocket.
Only taikonauts are allowed to ride (so far).

I would like to go for a ride on a Falcon 9 rocket.
Just flew their first two paying customers!

However I am _especially_ looking forward to a ride on the upcoming Starship rocket.
Starship it is! With Soyuz and Saturn V as backup options.

Happy hacking!