vertopal.com_Organizing a List
vertopal.com_Organizing a List
#To reverse the original order of a list, you can use the reverse()
method.
#You can quickly find the length of a list by using the len()
function.
# The list in this example has four items, so its length is 5
#Friends names in the list lets find out the length of list through
len() function
friends
len(friends)
####EXERCISE 2####
#Dinner Guests: Working with one of the programs from Exercises
#3-4 through 3-7 (pages 41–42), use len() to print a message
indicating
#the number of people you’re inviting to dinner.
# Original list of people you'd like to invite to dinner
guest_list = ["Albert Einstein", "Isaac Newton", "Ada Lovelace"]
len(guest_list)
# Inform about the bigger table
print("Good news! I just found a bigger dinner table, so I can invite
more guests!\n")
# Add three more guests
guest_list.insert(0, "Nikola Tesla") # Add to the beginning
guest_list.insert(2, "Galileo Galilei") # Add to the middle
guest_list.append("Leonardo da Vinci") # Add to the end
print(guest_list)
# Print the new set of invitation messages
for guest in guest_list:
print(f"Dear {guest},")
print("I would be honored to have you join me for dinner.")
print("Looking forward to an evening of great conversation!\n")
###EXERCISE 3
# Current list of people you'd like to invite to dinner
guest_list = ["Nikola Tesla", "Albert Einstein", "Galileo Galilei",
"Isaac Newton", "Ada Lovelace", "Leonardo da Vinci"]
len(guest_list)
# Inform about the change of plans
print("Unfortunately, the new dinner table won't arrive in time, so I
can only invite two people for dinner.\n")
# Remove guests until only two remain, apologizing to each
while len(guest_list) > 2:
removed_guest = guest_list.pop()
print(f"Sorry {removed_guest}, but I can't invite you to dinner
this time.\n")
# Print messages to the remaining two guests
for guest in guest_list:
print(f"Dear {guest},")
print("You are still invited to the dinner! Looking forward to
seeing you.\n")
# Remove the last two guests
del guest_list[0]
del guest_list[0]
# Print the list to confirm it's empty
print(f"Final guest list: {guest_list}")
###Exercise 5####
# Creating a list of rivers in Pakistan
rivers = ["indus", "chenab", "ravi", "jhelum", "sindh"]
# Display the list with title case for each river
rivers_title_case = [river.title() for river in rivers]
print("Rivers in title case:", rivers_title_case)
# Appending a new river to the list
rivers.append("kabul")
print("After appending a river:", rivers)
# Inserting a river at the second position
rivers.insert(1, "sutlej")
print("After inserting a river at index 1:", rivers)
# Sorting the list alphabetically
rivers.sort()
print("Alphabetically sorted rivers:", rivers)
# Sorting the list in reverse alphabetical order using sorted() and
reverse=True
sorted_rivers_reverse = sorted(rivers, reverse=True)
print("Reverse alphabetically sorted rivers using sorted():",
sorted_rivers_reverse)
# Reversing the original list
rivers.reverse()
print("Reversed original list:", rivers)
# Displaying the length of the list
length_of_rivers = len(rivers)
print("Number of rivers in the list:", length_of_rivers)
# Removing a specific river from the list by name
rivers.remove("indus")
print("After removing 'Indus':", rivers)
# Popping the last river from the list
popped_river = rivers.pop()
print(f"After popping the last river '{popped_river}':", rivers)
# Deleting a river at a specific position
del rivers[2]
print("After deleting the river at index 2:", rivers)
# Final state of the list
print("Final list of rivers:", rivers)
####EXERCISE 6#####
# Creating a list of cars
cars = ["toyota", "honda", "ford", "bmw", "audi"]
len(cars)
# Display the list with title case for each car
cars_title_case = [car.title() for car in cars]
print("Cars in title case:", cars_title_case)
# Appending a new car to the list
cars.append("mercedes")
print("After appending a car:", cars)
# Inserting a car at the second position
cars.insert(1, "chevrolet")
print("After inserting a car at index 1:", cars)
# Sorting the list alphabetically
cars.sort()
print("Alphabetically sorted cars:", cars)
# Sorting the list in reverse alphabetical order using sorted() and
reverse=True
sorted_cars_reverse = sorted(cars, reverse=True)
print("Reverse alphabetically sorted cars using sorted():",
sorted_cars_reverse)
# Reversing the original list
cars.reverse()
print("Reversed original list:", cars)
# Displaying the length of the list
length_of_cars = len(cars)
print("Number of cars in the list:", length_of_cars)
# Removing a specific car from the list by name
cars.remove("ford")
print("After removing 'Ford':", cars)
# Popping the last car from the list
popped_car = cars.pop()
print(f"After popping the last car '{popped_car}':", cars)
# Deleting a car at a specific position
del cars[2]
print("After deleting the car at index 2:", cars)
# Final state of the list
print("Final list of cars:", cars)
len(motorcycles)
print(motorcycles[-1])
print(motorcycles[-2])
print(motorcycles[-3])
print(motorcycles[2])