# Noemi Cabrera
# 1 November 2021
# In this lesson, I learned how to use the .extend(), +, .reverse(), reversed(), sorted(),
# and .sort() methids with lists. These are very helpful depending if you want to add a
# list to another, remove an item, sort alphabetically or numerically the items in a list,
# or reverse the order of the items in a list.
# I had difiulties with Task 3:sort() & sorted(). I coudn't print the cities that started
# with the letter Q or earlier. To fix this, I checked if the start index of each city was
# 'Q'or any letter before it using an if/else statement.
# [ In this code, the extend method is used to add two lists together to form a new list.
# The list of wish cities is added to the list of visited cities. This can be done with
# the + sign too. ] review and run example
visited_cities = ["New York", "Shanghai", "Munich", "Toyko", "Dubai", "Mexico City", "São Paulo", "Hyderabad"]
wish_cities = ["Reykjavík", "Moscow", "Beijing", "Lamu"]
# .extend()
# extending visited_cities list (IN PLACE) by concatenating wish_cities
visited_cities.extend(wish_cities)
print("ALL CITIES",visited_cities)
ALL CITIES ['New York', 'Shanghai', 'Munich', 'Toyko', 'Dubai', 'Mexico City', 'São Paulo', 'Hyderabad', 'Reykjavík', 'Moscow', 'Beijing', 'Lamu']
# [ In this code, the visited cities list and wish cities list are added together using
# the + sign and the new list is stored in the all_cities variable. Then, using the
# for..in loop, each item in the all_cities list is printed in a new line. ] review and run example
visited_cities = ["New York", "Shanghai", "Munich", "Toyko", "Dubai", "Mexico City", "São Paulo", "Hyderabad"]
wish_cities = ["Reykjavík", "Moscow", "Beijing", "Lamu"]
# (+) Addition operator for lists creates a (NEW) combined List
all_cities = visited_cities + wish_cities
print("ALL CITIES")
for city in all_cities:
print(city)
ALL CITIES
New York
Shanghai
Munich
Toyko
Dubai
Mexico City
São Paulo
Hyderabad
Reykjavík
Moscow
Beijing
Lamu
# [In this code , the lists tam_a & team_b are added together to form the new team_totals
# list using the + sign. The originals list and the new list are printed. ] review and run example
team_a = [0,2,2,2,4,4,4,5,6,6,6]
team_b = [0,0,0,1,1,2,3,3,3,6,8]
print("Team A:", team_a, "\nTeam B:",team_b)
# (+) Addition operator
team_totals = team_a + team_b
print("Team Totals", team_totals)
Team A: [0, 2, 2, 2, 4, 4, 4, 5, 6, 6, 6]
Team B: [0, 0, 0, 1, 1, 2, 3, 3, 3, 6, 8]
Team Totals [0, 2, 2, 2, 4, 4, 4, 5, 6, 6, 6, 0, 0, 0, 1, 1, 2, 3, 3, 3, 6, 8]
# [ In this code, the team_a list is added team_b to form a new or modified list with the
# extend method. The new list is printed. ] review and run example after running cell above
# .extend()
team_a.extend(team_b)
print("Team_a extended", team_a)
# what happens if you keep running this cell?
# If you keep running this cell the team_a list keeps extending wibecause the list team_b
# is added each time,
Team_a extended [0, 2, 2, 2, 4, 4, 4, 5, 6, 6, 6, 0, 0, 0, 1, 1, 2, 3, 3, 3, 6, 8]
# [ In this code, the common_birds list and the birds_seen list are combined using the
# extend method. The common_birds list is modified by adding the items from the bird_seen
# list. The updated list is printed.] extend the list common_birds with list birds_seen
# which you must create
common_birds = ["chicken", "blue jay", "crow", "pigeon"]
birds_seen = ["owl","eagle","hummingbird","gull"]
common_birds.extend(birds_seen)
print("Updated bird list:", common_birds)
Updated bird list: ['chicken', 'blue jay', 'crow', 'pigeon', 'owl', 'eagle', 'hummingbird', 'gull']
# In this code, the zero_nine and ten_onehundred lists are deifned with only integer data.
# These are added together by using the + sign and astored in the all_num variable.
# The new list is printed.
# [ ] Create 2 lists zero_nine and ten_onehundred that contain 1-9, and 10 - 100 by 10's.
# [ ] use list addition to concatenate the lists into all_num and print
zero_nine = [1,2,3,4,5,6,7,8,9]
ten_onehundred = [10,20,30,40,50,60,70,80,90,100 ]
all_num = zero_nine + ten_onehundred
print(all_num)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# [ In this code, the list of cities is reversed in place by using the .reverse() method.
# The list itself is modified and you cannot access the orginal one once you reverse it.]
# review and run example
cities_1 = ["Dubai", "Mexico City", "São Paulo", "Hyderabad"]
print("regular", cities_1)
cities_1.reverse()
print("reversed", cities_1)
regular ['Dubai', 'Mexico City', 'São Paulo', 'Hyderabad']
reversed ['Hyderabad', 'São Paulo', 'Mexico City', 'Dubai']
# [ In this code, the list all_num made up of integers is reversed with the .reverse() method. Then, a for..in loop is used to find the multiples
# of 3 in the the reverse list. The if/else checks if the numbers are divisible by 3 and
# prints the ones that are multiples of 3. ] review and run example
all_num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
print("regular list",all_num, "\n")
all_num.reverse()
print("reverse list",all_num, "\n")
num_len = len(all_num)
print("Three Multiple")
for num in all_num:
if num/3 == int(num/3):
print(num)
else:
pass
regular list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
reverse list [100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Three Multiple
90
60
30
9
6
3
0
# [ In this code, the count _list is created by casting the range 0-20 into a list.
# Then this list is reversed using the .reverse() method and printed using print() ]
# review and run example
# create a list of numbers by casting a range
count_list = list(range(21))
print("before list", count_list)
# and reverse
count_list.reverse()
print("after list", count_list)
before list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
after list [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
# In this code, the five_m list contains the integers that are multiples of 5 from 5 to 100.
# The original list is printed and then this list is reversed by using the reverse()
# method. The five_m modified list is printed.
# [ ] create and print a list of multiples of 5 from 5 to 100
# { ] reverse the list and print
five_m = [5,10,15,20,25,30,35,30,45,50,60,70,80,85,90,95,100]
print("orginal list:",five_m, "\n")
five_m.reverse()
print("reverse list",five_m, "\n")
orginal list: [5, 10, 15, 20, 25, 30, 35, 30, 45, 50, 60, 70, 80, 85, 90, 95, 100]
reverse list [100, 95, 90, 85, 80, 70, 60, 50, 45, 30, 35, 30, 25, 20, 15, 10, 5]
# In this code, the fours & more_fours lists contains the integers that are multiples of 4
# from 4 to 44. To create a mirror effect in the output, I reverse the fours_list and then
# combined the two lists together by using the + sign. The all_fours list is printed.
# [ ] Create two lists: fours & more_fours containing multiples of four from 4 to 44
# [ ] combine and print so that the output is mirrored [44, 40,...8, 4, 4, 8, ...40, 44]
fours = [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44]
more_fours = [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44]
fours.reverse()
all_fours = fours + more_fours
print(all_fours)
[44, 40, 36, 32, 28, 24, 20, 16, 12, 8, 4, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44]
# [ In this code, the quiz_scores list, which contains a list of integers, is sorted
# numerically from smallest to largest number. The .sort() method changes the original
# list, so we cannot access the original list again. ] review and run example
quiz_scores = [20, 19, 20, 15, 20, 20, 20, 18, 18, 18, 19]
# use .sort()
quiz_scores.sort()
print("quiz_scores:", quiz_scores)
quiz_scores: [15, 18, 18, 18, 19, 19, 20, 20, 20, 20, 20]
# [ IN this code, the srted() function is used with the game_points integer list. This
# function doesn't change the orginal list, so we can ccess the orginal later. The new
# sorted list, sorted_points, is created. ] review and run example
game_points = [3, 14, 0, 8, 21, 1, 3, 8]
# use sorted()
sorted_points = sorted(game_points)
print("game_points:", game_points)
print("sorted_points:", sorted_points)
game_points: [3, 14, 0, 8, 21, 1, 3, 8]
sorted_points: [0, 1, 3, 3, 8, 8, 14, 21]
# [ In this code, the cities_1 list, which contains a list of strings, is sorted
# alphabetically. The .sort() method changes the original list, so we cannot access the
# original list again. ] review and run example
cities_1 = ["Dubai", "Mexico City", "São Paulo", "Hyderabad"]
print("Unsorted", cities_1)
cities_1.sort()
print("Sorted", cities_1)
Unsorted ['Dubai', 'Mexico City', 'São Paulo', 'Hyderabad']
Sorted ['Dubai', 'Hyderabad', 'Mexico City', 'São Paulo']
# [ ] print cites from visited_cities list in alphbetical order using .sort()
# [ ] only print cities that names start "Q" or earlier
visited_cities = ["New York", "Shanghai", "Munich", "Toyko", "Dubai", "Mexico City", "São Paulo", "Hyderabad"]
visited_cities.sort()
print("List in alphabetical order:", visited_cities)
for city in visited_cities:
if city[0] <= "Q":
print(city)
else:
pass
List in alphabetical order: ['Dubai', 'Hyderabad', 'Mexico City', 'Munich', 'New York', 'Shanghai', 'São Paulo', 'Toyko']
Dubai
Hyderabad
Mexico City
Munich
New York
# In this code, a sorted version of the visited cites list is created and stored in
# sorted_cities. Using a for..in loop, I checked if the lenght of each city was 5 or less.
# If it was, the city was removed from the srted_cities list using the .remove() method.
# If not, they were kept in the list.
# [ ] make a sorted copy (sorted_cities) of visited_cities list
# [ ] remove city names 5 characters or less from sorted_cities
# [ ] print visitied cites and sorted cities
visited_cities = ["New York", "Shanghai", "Munich", "Toyko", "Dubai", "Mexico City", "São Paulo", "Hyderabad"]
sorted_cities = sorted(visited_cities)
for city in visited_cities:
if len(city) <= 5:
sorted_cities.remove(city)
else:
pass
print(visited_cities)
print(sorted_cities)
['New York', 'Shanghai', 'Munich', 'Toyko', 'Dubai', 'Mexico City', 'São Paulo', 'Hyderabad']
['Hyderabad', 'Mexico City', 'Munich', 'New York', 'Shanghai', 'São Paulo']
# [ ] build a list (add_animals) using a while loop, stop adding when an empty string is entered
add_animals = []
while True:
animal = input("Enter an animal name: ").title()
if animal != '':
add_animals.append(animal)
else:
break
# [ ] extend the lists into animals, then sort
animals = ["Chimpanzee", "Panther", "Wolf", "Armadillo"]
animals.extend(add_animals)
animals.sort()
# [ ] get input if list should be viewed alpha or reverse alpha and display list
a_or_r = input("Enter 'alpha' for alphabetical order or 'reverse' for reverse order:")
if a_or_r == 'alpha':
print(animals)
else:
animals.reverse()
print(animals)
['Armadillo', 'Cat', 'Chimpanzee', 'Dog', 'Panther', 'Wolf']
# This program gets user input for an animal name repeatedly until a blank string is
# entered. The names entered are added to an existing list. Then, the user is asked to
# enter if he wants the list in just alphabetical order or reversed. The final list is
# printed.
add_animals = []
while True:
animal = input("Enter an animal name or '' to finish: ").title()
if animal != '':
add_animals.append(animal)
else:
break
animals = ["Chimpanzee", "Panther", "Wolf", "Armadillo"]
animals.extend(add_animals)
animals.sort()
a_or_r = input("Enter 'alpha' for alphabetical order list or 'reverse' for reverse order list:")
if a_or_r == 'alpha':
print(animals)
else:
animals.reverse()
print(animals)
['Wolf', 'Panther', 'Chimpanzee', 'Bunn', 'Bird', 'Armadillo']