<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 These are the search queries for the Spotify Web API
Step1:  1) With "Lil Wayne" and "Lil Kim" there are a lot of "Lil" musicians. Do a search and print a list of 50 that are playable in the USA (or the country of your choice), along with their popularity score.
Step2:  2 a) What genres are most represented in the search results?
Finding all the genres and combining into one list.
Step3:  Counting the genres.
Step4:  Sorting the genres by occurences.
Step5:  2 b) Edit your previous printout to also display a list of their genres in the format "GENRE_1, GENRE_2, GENRE_3". If there are no genres, print "No genres listed".
Step6:  3 a) Use a for loop to determine who BESIDES Lil Wayne has the highest popularity rating.
Step7:  3 b) Is it the same artist who has the largest number of followers?
Step8:  Creating a list of the popularity values, so we can sort them and say which one is the highest)
Step9:  Deciding which one is highest
Step10:  4) Print a list of Lil's that are more popular than Lil' Kim.
Establishing how high Lil' Kim's popularity is. Would this be possible in one go?
Step11:  5) Pick two of your favorite Lils to fight it out, and use their IDs to print out their top tracks.
Tip
Step12:  6 Will the world explode if a musicians swears? Get an average popularity for their explicit songs vs. their non-explicit songs. How many minutes of explicit songs do they have? Non-explicit?
Number of Explicit Tracks for Lil Scrappy.
Step13:  And this is the same for Lil Mama
Step14:  7 a) Since we're talking about Lils, what about Biggies? How many total "Biggie" artists are there? How many total "Lil"s? If you made 1 request every 5 seconds, how long would it take to download information on all the Lils vs the Biggies?
Step15:  8) Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?
<ASSISTANT_TASK:>
Python Code:
response = requests.get('https://api.spotify.com/v1/search?query=Lil&type=artist&limit=50&market=US')
Lil_data = response.json()
Lil_data.keys()
Lil_data['artists'].keys()
Explanation: These are the search queries for the Spotify Web API
End of explanation
Lil_artists = Lil_data['artists']['items']
for artist in Lil_artists:
    print(artist['name'], artist['popularity'])
Explanation: 1) With "Lil Wayne" and "Lil Kim" there are a lot of "Lil" musicians. Do a search and print a list of 50 that are playable in the USA (or the country of your choice), along with their popularity score.
End of explanation
Lil_artists = Lil_data['artists']['items']
for artist in Lil_artists:
    print(artist['name'], artist['popularity'])
    #joining
    if len(artist['genres']) == 0:
        print("No genres listed")
    else:
        genres = ", ".join(artist['genres'])
        print("Genres: ", genres)
    
    
Lil_artists = Lil_data['artists']['items']
Lil_genres_list = []
for genres in Lil_artists:
    Lil_genres_list = genres["genres"] + Lil_genres_list
print(Lil_genres_list)
    
Explanation: 2 a) What genres are most represented in the search results?
Finding all the genres and combining into one list.
End of explanation
Genre_list = [[x,Lil_genres_list.count(x)] for x in set(Lil_genres_list)]
print(Genre_list)
Explanation: Counting the genres.
End of explanation
sorted(Genre_list, key = lambda x: int(x[1]), reverse=True)
Sorted_by_occurences_Genre_list = sorted(Genre_list, key = lambda x: int(x[1]), reverse=True)
print("The most frequent genre of the musicians called Lil is", Sorted_by_occurences_Genre_list[0])
Explanation: Sorting the genres by occurences.
End of explanation
Lil_artists = Lil_data['artists']['items']
for artist in Lil_artists:
    if artist['genres'] == []:
        print(artist['name'], artist['popularity'], "No genres listed.")
    else:
        print(artist['name'], artist['popularity'], artist['genres'])
Explanation: 2 b) Edit your previous printout to also display a list of their genres in the format "GENRE_1, GENRE_2, GENRE_3". If there are no genres, print "No genres listed".
End of explanation
for artist in Lil_artists: 
    if artist['popularity'] >= 72 and artist['name'] != 'Lil Wayne':
            print(artist['name'])
#Better solution:
most_popular_name = ""
most_popular_score = 0
for artist in Lil_artists:
    #print("Comparing", artist['popularity'], 'to', most_popular_score)
    if artist['popularity'] > most_popular_score:
        print("checking for Lil Wayne")
        if artist['name'] == 'Lil Wayne':
            print('go away')
        else:
        #The change you are keeping track of
        #a.k.a. what you are keeping track of
            print('not Lil Wayne, updating our notebook')
            most_popular_name = artist['name']
            most_popular_score = artist['popularity']
            
print(most_popular_name, most_popular_score)
####### This doesn't work
#name = 'Lil Soma'
#target_score = 72
#1 INITIAL CONDITION
#second_best_artists = []
#second_best_artists = [Lil Yachty]
#Aggregation Problem
#When you're looping through a series of serious objects
#and sometimes you want to add one of those objects
#to a different list
#for artist in artists:
#    print('Looking at', artist['name'])
    #2 COndition
    #wehen we want someone on the list
#    if artist['popularity'] == 72:
#        print('!!! The artist is popularity is 72.')
#        second_best_artists.append(second_best_artists)
        
    
Lil_data['artists'].keys()
for artist in Lil_artists:
    if artist['name'] == ["Lil Wayne"]:
        print(artist['popularity'], "is")
Explanation: 3 a) Use a for loop to determine who BESIDES Lil Wayne has the highest popularity rating.
End of explanation
type(artist['followers'])
artist['followers']
Explanation: 3 b) Is it the same artist who has the largest number of followers?
End of explanation
Lil_artists = Lil_data['artists']['items']
List_of_Followers = []
for artist in Lil_artists:
    List_of_Followers.append(artist['followers']['total'])
print(List_of_Followers)
Explanation: Creating a list of the popularity values, so we can sort them and say which one is the highest)
End of explanation
List_of_Followers.sort(reverse=True)
print(List_of_Followers)
Highest_Number_of_Followers = (List_of_Followers[0])
print(Highest_Number_of_Followers)
for artist in Lil_artists: 
    if artist['followers']['total'] > List_of_Followers[0] and artist['name'] != 'Lil Wayne':
        print(artist['name'], "has more followers than Lil Wayne.")
    else:
        print("Their are no artists with more followers that Lil Wayne.")
        break
Explanation: Deciding which one is highest:
End of explanation
for artist in Lil_artists: 
    if artist['name'] == "Lil' Kim":
        print(artist['popularity'])
for artist in Lil_artists: 
    if artist['popularity'] > 62:
        print(artist['name'], artist['popularity'])
Explanation: 4) Print a list of Lil's that are more popular than Lil' Kim.
Establishing how high Lil' Kim's popularity is. Would this be possible in one go?
End of explanation
for artist in Lil_artists:
    print(artist['name'], artist['id'])
response = requests.get('https://api.spotify.com/v1/artists/5einkgXXrjhfYCyac1FANB/top-tracks?country=US')
Lil_Scrappy_data = response.json()
type(Lil_Scrappy_data)
response = requests.get('https://api.spotify.com/v1/artists/5qK5bOC6wLtuLhG5KvU17c/top-tracks?country=US')
Lil_Mama_data = response.json()
type(Lil_Mama_data)
Lil_Scrappy_data.keys()
Lil_Mama_data.keys()
type(Lil_Scrappy_data.keys())
type(Lil_Mama_data.keys())
Scrappy_tracks = Lil_Scrappy_data['tracks']
for tracks in Scrappy_tracks:
    print(tracks['name'])
Mama_tracks = Lil_Mama_data['tracks']
for tracks in Mama_tracks:
    print(tracks['name'])
Explanation: 5) Pick two of your favorite Lils to fight it out, and use their IDs to print out their top tracks.
Tip: You're going to be making two separate requests, be sure you DO NOT save them into the same variable.
End of explanation
explicit_track_scrappy = 0
non_explicit_track_scrappy = 0
unknown_scrappy = 0
for tracks in Scrappy_tracks:
    if tracks['explicit'] == True:
        explicit_track_scrappy = explicit_track_scrappy + 1
    elif tracks['explicit'] == False:
        non_explicit_track_scrappy = non_explicit_track_scrappy + 1
    else:
        unknown_scrappy = unknown_scrappy + 1
explicit_track_pop_total = 0
non_explicit_track_pop_total = 0
for tracks in Scrappy_tracks:
    if tracks['explicit'] == True:
        explicit_track_pop_total = explicit_track_pop_total + tracks['popularity']
    elif tracks['explicit'] == False:
        non_explicit_track_pop_total = non_explicit_track_pop_total + tracks['popularity']
explicit_track_duration_total = 0
non_explicit_track_duration_total = 0
for tracks in Scrappy_tracks:
    if tracks['explicit'] == True:
        explicit_track_duration_total = explicit_track_duration_total + tracks['duration_ms']
    elif tracks['explicit'] == False:
        non_explicit_track_duration_total = non_explicit_track_duration_total + tracks['duration_ms']
    
print("The average rating of explicit songs by Lil Scrappy is", round(explicit_track_pop_total / explicit_track_scrappy), ".")
print("The average rating of non-explicit songs by Lil Scrappy is", round(non_explicit_track_pop_total / non_explicit_track_scrappy), ".")
print("The duration of explicit song material of Lil Scrappy is", round(explicit_track_duration_total / 1000), "minutes, and of non explicit material is", round(non_explicit_track_duration_total / 1000), "minutes.") 
Explanation: 6 Will the world explode if a musicians swears? Get an average popularity for their explicit songs vs. their non-explicit songs. How many minutes of explicit songs do they have? Non-explicit?
Number of Explicit Tracks for Lil Scrappy.
End of explanation
explicit_track_Mama = 0
non_explicit_track_Mama = 0
unknown = 0
for tracks in Mama_tracks:
    if tracks['explicit'] == True:
        explicit_track_Mama = explicit_track_Mama + 1
    elif tracks['explicit'] == False:
        non_explicit_track_Mama = non_explicit_track_Mama + 1
    else:
        unknown = unknown + 1
explicit_track_pop_total_Mama = 0
non_explicit_track_pop_total_Mama = 0
for tracks in Mama_tracks:
    if tracks['explicit'] == True:
        explicit_track_pop_total_Mama = explicit_track_pop_total_Mama + tracks['popularity']
    elif tracks['explicit'] == False:
        non_explicit_track_pop_total_Mama = non_explicit_track_pop_total_Mama + tracks['popularity']
        
explicit_track_duration_total_Mama = 0
non_explicit_track_duration_total_Mama = 0
for tracks in Mama_tracks:
    if tracks['explicit'] == True:
        explicit_track_duration_total_Mama = explicit_track_duration_total_Mama + tracks['duration_ms']
    elif tracks['explicit'] == False:
        non_explicit_track_duration_total_Mama = non_explicit_track_duration_total_Mama + tracks['duration_ms']        
        
print("The average rating of explicit songs by Lil Mama is", round(explicit_track_pop_total_Mama / explicit_track_Mama), ".")
print("The average rating of non-explicit songs by Lil Mama is", round(non_explicit_track_pop_total_Mama / non_explicit_track_Mama), ".")
print("The duration of explicit song material of Lil Mama is", round(explicit_track_duration_total_Mama / 1000), "minutes, and of non explicit material is", round(non_explicit_track_duration_total_Mama / 1000), "minutes.") 
Explanation: And this is the same for Lil Mama:
End of explanation
response = requests.get('https://api.spotify.com/v1/search?query=Biggie&type=artist&limit=50&market=US')
Biggie_data = response.json()
response = requests.get('https://api.spotify.com/v1/search?query=Lil&type=artist&limit=50&market=US')
Lil_data = response.json()
Biggie_artists = Biggie_data['artists']['total']
Lil_artists = Lil_data['artists']['total']
print("There are", Biggie_artists, "artists named Biggie on Spotify and", Lil_artists, "named Lil",)
Total_Download_Time_Biggie = Biggie_artists / 50 * 5
Total_Download_Time_Lil = Lil_artists / 50 * 5
print("It would take", round(Total_Download_Time_Biggie), "seconds to download all the Biggie artists and", round(Total_Download_Time_Lil), "seconds to download the Lil artists." )
Explanation: 7 a) Since we're talking about Lils, what about Biggies? How many total "Biggie" artists are there? How many total "Lil"s? If you made 1 request every 5 seconds, how long would it take to download information on all the Lils vs the Biggies?
End of explanation
Lil_artists_popularity = Lil_data['artists']['items']
popularity_total = 0
for popularity in Lil_artists_popularity:
    popularity_total = popularity_total + popularity['popularity']
print("The average rating for the top 50 artists called Lil is:", round(popularity_total / 50))
Biggie_artists_popularity = Biggie_data['artists']['items']
Biggie_popularity_total = 0
for popularity2 in Biggie_artists_popularity:
    Biggie_popularity_total = Biggie_popularity_total + popularity2['popularity']
print("The average rating for the top 50 artists called Biggie is:", round(Biggie_popularity_total / 49) )
Lil_artists_popularity = Lil_data['artists']['items']
for popularity in Lil_artists_popularity:
    print(popularity['name'], popularity['popularity'])
Biggie_popularity = Biggie_data['artists']['items']
for artist in Biggie_popularity:
    print(artist['name'], artist['popularity'])
import csv
with open('Biggie.csv', 'w') as mycsvfile:
    thedatawriter = csv.writer(mycsvfile)
    for artist in Biggie_popularity:
        thedatawriter.writerow(artist['name'])
        thedatawriter.writerow(artist['popularity'])
Explanation: 8) Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?
End of explanation
<END_TASK>