Question
Suppose a music streaming service uses two lists to maintain a user's favorite songs and the corresponding ratings, respectively: songs = ['Back to Black', 'Poker
Suppose a music streaming service uses two lists to maintain a user's favorite songs and the corresponding ratings, respectively:
songs = ['Back to Black', 'Poker Face', 'Yellow', 'Lolipop', 'All Too Well', 'Delicate', 'Moves Like Jagger'] ratings = [9.5, 8, 9, 8, 10, 7.5, 8]
Write code without using loop statements to create a playlist, which orders the user's favorite songs based on their ratings. And if a tie is observed, break the tie by sorting song titles lexicographically.
The expected output should look like the following:
['All Too Well', 'Back to Black', 'Yellow', 'Lolipop', 'Moves Like Jagger', 'Poker Face', 'Delicate']
Steps:
First, try to come up with the following list with zip() and sorted():
[(10, 'All Too Well'), (9.5, 'Back to Black'), (9, 'Yellow'), (8, 'Lolipop'), (8, 'Moves Like Jagger'), (8, 'Poker Face'), (7.5, 'Delicate')]
Include only the 2nd part of each tuple above in the output list with a comprehension or zip()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started