Skip to content

Instantly share code, notes, and snippets.

  • Save LizaPiya/f4c51a2c603b3a0af60903e1c65e08b0 to your computer and use it in GitHub Desktop.
Save LizaPiya/f4c51a2c603b3a0af60903e1c65e08b0 to your computer and use it in GitHub Desktop.
Tuples
## The .items() method produces a sequence of key-value pair tuples. With this in mind, write code to create a list of keys from the dictionary track_medal_counts and assign the list to the variable name track_events. Do NOT use the .keys() method.
track_medal_counts = {'shot put': 1, 'long jump': 3, '100 meters': 2, '400 meters': 2, '100 meter hurdles': 3, 'triple jump': 3, 'steeplechase': 2, '1500 meters': 1, '5K': 0, '10K': 0, 'marathon': 0, '200 meters': 0, '400 meter hurdles': 0, 'high jump': 1}
track_keys=[]
for i in track_medal_counts.items():
track_keys.append(i[0])
track_events=track_keys
##Create a tuple called olympics with four elements: “Beijing”, “London”, “Rio”, “Tokyo”.
olympics=("Beijing","London","Rio","Tokyo")
## 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')]
second_element=[]
for i in tuples_lst:
second_element.append(i[1])
country=second_element
## 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
## Define a function called info with five parameters: name, gender, age, bday_month, and hometown. The function should then return a tuple with all five parameters in that order.
def info(name,gender,age,bday_month,hometown):
return (name,gender,age,bday_month,hometown)
## 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 i in gold.items():
num_medals.append(i[1])
## 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 i in tups:
seconds.append(i[1])
## Define a function called info with the following required parameters: name, age, birth_year, year_in_college, and hometown. The function should return a tuple that contains all the inputted information.
def info(name,age,birth_year,year_in_college,hometown):
return (name,age,birth_year,year_in_college,hometown)
## Define a function called information that takes as input, the variables name, birth_year, fav_color, and hometown. It should return a tuple of these variables in this order.
def information(a,b,c,d):
return a,b,c,d
information("name","birth_year","fav_color","hometown")
fruits = ['apple', 'pear', 'apricot', 'cherry', 'peach']
for item in enumerate(fruits): ##In each tuple, the first element is an integer and the second is an item from the original sequence
print(item[0], item[1])
output:
0 apple
1 pear
2 apricot
3 cherry
4 peach
##Another enumerate example
fruits = ['apple', 'pear', 'apricot', 'cherry', 'peach']
for idx, fruit in enumerate(fruits):
print(idx, fruit)
output:
0 apple
1 pear
2 apricot
3 cherry
4 peach
## 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 i in lst_tups:
t_check.append(i[2])
## def circleInfo(r):
""" Return (circumference, area) of a circle of radius r """
c = 2 * 3.14159 * r
a = 3.14159 * r * r
return c, a
print(circleInfo(10))
circumference, area = circleInfo(10)
print(circumference)
print(area)
circumference_two, area_two = circleInfo(45)
print(circumference_two)
print(area_two)
## Create a tuple called practice that has four elements: ‘y’, ‘h’, ‘z’, and ‘x’.
practice=("y", "h", "z","x")
def add(x, y):
return x + y
print(add(3, 4))
z = (5, 4)
print(add(*z)) # this line will cause the values to be unpacked
output:
7
9
##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 i in pokemon.items():
p_names.append(i[0])
p_number.append(i[1])
## With only one line of code, assign the variables water, fire, electric, and grass to the values “Squirtle”, “Charmander”, “Pikachu”, and “Bulbasaur”
(water,fire,electric,grass)=("Squirtle","Charmander","Pikachu","Bulbasaur")
@hadrocodium
Copy link

hadrocodium commented Apr 2, 2022

"""
The .items() method produces a sequence of key-value pair tuples. With this in mind, write code to create a list of keys from the dictionary track_medal_counts and assign the list to the variable name track_events. Do NOT use the .keys() method.
"""


track_medal_counts = {'shot put': 1, 'long jump': 3, '100 meters': 2, '400 meters': 2, '100 meter hurdles': 3, 'triple jump': 3, 'steeplechase': 2, '1500 meters': 1, '5K': 0, '10K': 0, 'marathon': 0, '200 meters': 0, '400 meter hurdles': 0, 'high jump': 1}


#  tup_key_value_pair is tuple because items method produces the tuples in which the first element is key, and the second element is value
track_events = [tup_key_value_pair[0] for tup_key_value_pair in track_medal_counts.items()]
print(track_events)

tup_key_value_pair[0] gets key from key-value tuple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment