Skip to content

Instantly share code, notes, and snippets.

@guestPK1986
Created August 23, 2020 02:13
Show Gist options
  • Save guestPK1986/cb5c6121fc9ab05553d7b228080fc1f1 to your computer and use it in GitHub Desktop.
Save guestPK1986/cb5c6121fc9ab05553d7b228080fc1f1 to your computer and use it in GitHub Desktop.
Tuples
#Provided is a list of tuples. Create another list called t_check that contains the third element of every tuple.
lst_tups = [('Articuno', 'Moltres', 'Zaptos'), ('Beedrill', 'Metapod', 'Charizard', 'Venasaur', 'Squirtle'), ('Oddish', 'Poliwag', 'Diglett', 'Bellsprout'), ('Ponyta', "Farfetch'd", "Tauros", 'Dragonite'), ('Hoothoot', 'Chikorita', 'Lanturn', 'Flaaffy', 'Unown', 'Teddiursa', 'Phanpy'), ('Loudred', 'Volbeat', 'Wailord', 'Seviper', 'Sealeo')]
t_check = []
for x in lst_tups:
t_check.append(x[2])
print (t_check)
#Below, we have provided a list of tuples.
#Write a for loop that saves the second element of each tuple into a list called seconds
tups = [('a', 'b', 'c'), (8, 7, 6, 5), ('blue', 'green', 'yellow', 'orange', 'red'), (5.6, 9.99, 2.5, 8.2), ('squirrel', 'chipmunk')]
seconds = []
for each in tups:
seconds.append(each[1])
print(seconds)
#we have provided you a dictionary called pokemon.
#For every key value pair, append the key to the list p_names, and append the value to the list p_number.
#Do not use the .keys() or .values() methods.
pokemon = {'Rattata': 19, 'Machop': 66, 'Seel': 86, 'Volbeat': 86, 'Solrock': 126}
p_names = []
p_number = []
for x in pokemon.items():
p_names.append(x[0])
p_number.append(x[1])
#The list below, tuples_lst, is a list of tuples.
#Create a list of the second elements of each tuple and assign this list to the variable country
tuples_lst = [('Beijing', 'China', 2008), ('London', 'England', 2012), ('Rio', 'Brazil', 2016, 'Current'), ('Tokyo', 'Japan', 2020, 'Future')]
country = []
for x in tuples_lst:
country.append(x[1])
#With only one line of code, assign the variables city, country, and year to the values of the tuple olymp
olymp = ('Rio', 'Brazil', 2016)
city, country, year = olymp
#Given is the dictionary, gold, which shows the country and the number of gold medals
#they have earned so far in the 2016 Olympics.
#Create a list, num_medals, that contains only the number of medals for each country.
#You must use the .items() method. Note: The .items() method provides a list of tuples. Do not use .keys() method.
gold = {'USA':31, 'Great Britain':19, 'China':19, 'Germany':13, 'Russia':12, 'Japan':10, 'France':8, 'Italy':8}
num_medals = []
for x in gold.items():
num_medals.append(x[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment