Challenge: Zip and Sort Functions
We still have a few functions from the previous chapter to cover, so let's get some practice with them.
Recall the zip()
function, which is particularly useful when working with multiple related lists. This function allows you to manage different properties of items simultaneously, such as sorting products while maintaining their associated attributes.
Swipe to start coding
You got three lists: product names, prices, and quantities sold. Your goal is to organize, sort, and display this data in a specific format.
Code Instructions
- Use
zip()
to combine the three lists into a list of tuples in the order:(product_name, price, quantity_sold)
. Assign the result tocombined_list
. - Use
sorted()
to sortcombined_list
by product name in ascending order. Assign the sorted result tosorted_products
. - Loop through
sorted_products
and print each product's name, price, and quantity sold using the specified format.
Output Requirements
For each product, print:
Product: <product_name>, Price: <product_price>, Quantity Sold: <quantity_sold>
Soluzione
Grazie per i tuoi commenti!
single
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Awesome!
Completion rate improved to 2.17
Challenge: Zip and Sort Functions
Scorri per mostrare il menu
We still have a few functions from the previous chapter to cover, so let's get some practice with them.
Recall the zip()
function, which is particularly useful when working with multiple related lists. This function allows you to manage different properties of items simultaneously, such as sorting products while maintaining their associated attributes.
Swipe to start coding
You got three lists: product names, prices, and quantities sold. Your goal is to organize, sort, and display this data in a specific format.
Code Instructions
- Use
zip()
to combine the three lists into a list of tuples in the order:(product_name, price, quantity_sold)
. Assign the result tocombined_list
. - Use
sorted()
to sortcombined_list
by product name in ascending order. Assign the sorted result tosorted_products
. - Loop through
sorted_products
and print each product's name, price, and quantity sold using the specified format.
Output Requirements
For each product, print:
Product: <product_name>, Price: <product_price>, Quantity Sold: <quantity_sold>
Soluzione
Grazie per i tuoi commenti!
Awesome!
Completion rate improved to 2.17single