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.


In [50]:
import requests

response = requests.get('https://api.spotify.com/v1/search?query=artist:lil&type=artist&market=us&limit=50')

data = response.json()

artists = data['artists']['items']

for artist in artists:
    print(artist['name'], artist['popularity'])


Lil Wayne 86
Lil Yachty 72
Lil Uzi Vert 72
Lil Dicky 68
Boosie Badazz 67
Lil Jon 72
King Lil G 61
Lil Durk 60
Lil Jon & The East Side Boyz 60
Lil Bibby 54
G Herbo 53
Lil Rob 50
Lil Reese 50
Lil Keke 48
Bow Wow 57
Lil Scrappy 49
Lil Wyte 50
Lil Blood 45
Lil Snupe 45
Lil Mama 45
Lil B 44
Lil' Kim 62
Lil Cuete 40
Lil Phat 39
Lil Debbie 43
Lil Twist 39
Lil Trill 37
Lil Twon 38
Lil AJ 37
Lil Lonnie 36
Lil Goofy 35
Mr. Lil One 36
Lil Flash 38
Lil Kesh 39
Lil Haiti 35
Lil Silva 43
Lil Rue 34
Lil Cray 35
Lil Eddie 41
Lil Wayne, DJ Drama 35
Lil Yase 33
Lil Suzy 34
Lil Mouse 34
Lil C 33
Lil Rick 38
Lil Boom 33
Lil June 32
Lil E 34
Lil Fate 34
Lil' Flip 49

2) What genres are most represented in the search results? 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".


In [41]:
for artist in artists:
    if not artist['genres']:
        print(artist['name'], artist['popularity'], 'No genres listed')
    else:
        print(artist['name'], artist['popularity'], ', '.join(artist['genres']))


Lil Wayne 86 dirty south rap, pop rap, southern hip hop, trap music
Lil Yachty 72 No genres listed
Lil Uzi Vert 72 No genres listed
Lil Dicky 68 No genres listed
Boosie Badazz 67 No genres listed
Lil Jon 72 crunk, dirty south rap, southern hip hop
King Lil G 61 No genres listed
Lil Durk 60 No genres listed
Lil Jon & The East Side Boyz 60 No genres listed
Lil Bibby 54 No genres listed
G Herbo 53 No genres listed
Lil Rob 50 chicano rap, latin hip hop
Lil Reese 50 No genres listed
Lil Keke 48 No genres listed
Bow Wow 57 hip pop, pop rap
Lil Scrappy 49 crunk, dirty south rap, southern hip hop, trap music
Lil Wyte 50 juggalo
Lil Blood 45 No genres listed
Lil Snupe 45 No genres listed
Lil Mama 45 hip pop
Lil B 44 No genres listed
Lil' Kim 62 hip pop
Lil Cuete 40 chicano rap
Lil Phat 39 No genres listed
Lil Debbie 43 No genres listed
Lil Twist 39 jerk
Lil Trill 37 deep trap
Lil Twon 38 No genres listed
Lil AJ 37 No genres listed
Lil Lonnie 36 No genres listed
Lil Goofy 35 No genres listed
Mr. Lil One 36 chicano rap
Lil Flash 38 No genres listed
Lil Kesh 39 No genres listed
Lil Haiti 35 No genres listed
Lil Silva 43 No genres listed
Lil Rue 34 No genres listed
Lil Cray 35 No genres listed
Lil Eddie 41 No genres listed
Lil Wayne, DJ Drama 35 No genres listed
Lil Yase 33 No genres listed
Lil Suzy 34 freestyle
Lil Mouse 34 No genres listed
Lil C 33 No genres listed
Lil Rick 38 soca
Lil Boom 33 No genres listed
Lil June 32 No genres listed
Lil E 34 No genres listed
Lil Fate 34 No genres listed
Lil' Flip 49 crunk, dirty south rap

In [213]:
genre_list = []
for artist in artists:
    for genre in artist['genres']:
        genre_list.append(genre)
sorted_genre = sorted(genre_list)
genre_list_number = range(len(sorted_genre))
genre_count = 0
for number in genre_list_number:
    if not sorted_genre[number] == sorted_genre[number - 1]:
        print((sorted_genre[number]), genre_list.count(sorted_genre[number]))
        if genre_count < genre_list.count(sorted_genre[number]):
            genre_count = genre_list.count(sorted_genre[number])
            freq_genre = sorted_genre[number]
print('')

print('With', genre_count, 'artists,', freq_genre, 'is the most represented in search results.')


chicano rap 3
crunk 3
deep trap 1
dirty south rap 4
freestyle 1
hip pop 3
jerk 1
juggalo 1
latin hip hop 1
pop rap 2
soca 1
southern hip hop 3
trap music 2

With 4 artists, dirty south rap is the most represented in search results.

In [ ]:
numbers = [72, 3, 0, 72, 34, 72, 3]

Tip: "how to join a list Python" might be a helpful search

3) Use a for loop to determine who BESIDES Lil Wayne has the highest popularity rating. Is it the same artist who has the largest number of followers?


In [35]:
highest_pop = 0
for artist in artists:
    if  artist['name'] != 'Lil Wayne' and highest_pop < artist['popularity']:
        highest_pop = artist['popularity']
        highest_pop_artist = artist['name']
print(highest_pop_artist, 'is the second-most-popular artist with \"Lil\" in his/her name.')

most_followers = 0
for artist in artists:
    if  most_followers < artist['followers']['total']:
        most_followers = artist['followers']['total']
        most_followers_artist = artist['name']
print(most_followers_artist, 'has', most_followers, 'followers.')

if highest_pop_artist == most_followers_artist:
    print('The second-most-popular \'Lil\' artist is also the one with the most followers.')
else:
    print('The second-most-popular \'Lil\' artist and the one with the most followers are different people.')


Lil Yachty is the second-most-popular artist with "Lil" in his/her name.
Lil Wayne has 2621796 followers.
The second-most-popular 'Lil' artist and the one with the most followers are different people.

4) Print a list of Lil's that are more popular than Lil' Kim.


In [39]:
for artist in artists:
    if artist['name'] == 'Lil\' Kim':
        more_popular = artist['popularity']
for artist in artists:
    if more_popular < artist ['popularity']:
        print(artist['name'], 'is more popular than Lil\' Kim with a popularity score of', artist['popularity'])


Lil Wayne is more popular than Lil' Kim with a popularity score of 86
Lil Yachty is more popular than Lil' Kim with a popularity score of 72
Lil Uzi Vert is more popular than Lil' Kim with a popularity score of 72
Lil Dicky is more popular than Lil' Kim with a popularity score of 68
Boosie Badazz is more popular than Lil' Kim with a popularity score of 67
Lil Jon is more popular than Lil' Kim with a popularity score of 72

5) Pick two of your favorite Lils to fight it out, and use their IDs to print out their top tracks.


In [61]:
wayne_id = '55Aa2cqylxrFIXC767Z865'



wayne_response = requests.get('https://api.spotify.com/v1/artists/' + wayne_id + '/top-tracks?country=us')

wayne_data = wayne_response.json()

print('Lil Wayne\'s top tracks:')
wayne_tracks = wayne_data['tracks']
for track in wayne_tracks:
    print(track['name'])
print('')



kim_id = '5tth2a3v0sWwV1C7bApBdX'

kim_response = requests.get('https://api.spotify.com/v1/artists/' + kim_id + '/top-tracks?country=us')

kim_data = kim_response.json()

print('Lil\' Kim\'s top tracks:')
kim_tracks = kim_data['tracks']
for track in kim_tracks:
    print(track['name'])


Lil Wayne's top tracks:
Forever
Nothing But Trouble - Instagram Models
6 Foot 7 Foot
A Milli
Right Above It
Lollipop
Love Me
Rich As Fuck
Mirror
Believe Me

Lil' Kim's top tracks:
Crush On You
Magic Stick
Magic Stick - Edited
#Mine (feat. Kevin Gates)
The Jump Off
Lighters Up - Nationwide Version
Big Momma Thang
M.A.F.I.A. Land
Queen B@#$H
Not Tonight

Tip: You're going to be making two separate requests, be sure you DO NOT save them into the same variable.

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?


In [79]:
print('Lil Wayne\'s explicit top tracks:')
ew_total_pop = 0
ew_total_tracks = 0
ew_playtime = 0
for track in wayne_tracks:
    if track['explicit']:
        ew_total_pop = ew_total_pop + track['popularity']
        ew_total_tracks = ew_total_tracks + 1
        ew_playtime = ew_playtime + track['duration_ms']/60000
if ew_total_tracks == 0:
    print('There are no explicit tracks.')
else:
    print('The average popularity is', ew_total_pop / ew_total_tracks)
print('He has', ew_playtime, 'minutes of explicit music in his top tracks.')   
print('')

print('Lil Wayne\'s non-explicit top tracks:')
nw_total_pop = 0
nw_total_tracks = 0
nw_playtime = 0
for track in wayne_tracks:
    if not track['explicit']:
        nw_total_pop = nw_total_pop + track ['popularity']
        nw_total_tracks = nw_total_tracks + 1
        nw_playtime = nw_playtime + track['duration_ms']/60000
if nw_total_tracks == 0:
    print('There are no non-explicit tracks.')
else:
    print('The average popularity is', nw_total_pop / nw_total_tracks)
print('He has', nw_playtime, 'minutes of non-explicit music in his top tracks.')
print('')

print('Lil\' Kim\'s explicit top tracks:')
ek_total_pop = 0
ek_total_tracks = 0
ek_playtime = 0
for track in kim_tracks:
    if track['explicit']:
        ek_total_pop = ek_total_pop + track ['popularity']
        ek_total_tracks = ek_total_tracks + 1
        ek_playtime = ek_playtime + track['duration_ms']/60000
if ek_total_tracks == 0:
    print('There are no explicit tracks.')
else:
    print('The average popularity is', ek_total_pop / ek_total_tracks)
print('She has', ek_playtime, 'minutes of explicit music in her top tracks.')
print('')

print('Lil\' Kim\'s non-explicit top tracks:')
nk_total_pop = 0
nk_total_tracks = 0
nk_playtime = 0
for track in kim_tracks:
    if not track['explicit']:
        nk_total_pop = nk_total_pop + track ['popularity']
        nk_total_tracks = nk_total_tracks + 1
        nk_playtime = nk_playtime + track['duration_ms']/60000
if nk_total_tracks == 0:
    print('There are no non-explicit tracks.')
else:
    print('The average popularity is', nk_total_pop / nk_total_tracks)
print('She has', nk_playtime, 'minutes of non-explicit music in her top tracks.')


Lil Wayne's explicit top tracks:
The average popularity is 69.5
He has 44.32301666666667 minutes of explicit music in his top tracks.

Lil Wayne's non-explicit top tracks:
There are no non-explicit tracks.
He has 0 minutes of non-explicit music in his top tracks.

Lil' Kim's explicit top tracks:
The average popularity is 46.77777777777778
She has 40.31313333333334 minutes of explicit music in her top tracks.

Lil' Kim's non-explicit top tracks:
The average popularity is 54.0
She has 5.57355 minutes of non-explicit music in her top tracks.

7) 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?


In [156]:
biggie_response = requests.get('https://api.spotify.com/v1/search?query=artist:biggie&type=artist&market=us&limit=50')
biggie_data = biggie_response.json()

biggie_artists = biggie_data['artists']['items']

total_biggies = 0
for artist in biggie_artists:
    total_biggies = total_biggies + 1
print('There are', total_biggies, 'Biggies on Spotify.')
print('It would take', total_biggies * 5, 'seconds to request all of the Biggies if you were requesting one every five seconds.')
print('')


There are 47 Biggies on Spotify.
It would take 235 seconds to request all of the Biggies if you were requesting one every five seconds.


In [159]:
pages = range(90)
total_lils = 0
for page in pages:
    lil_response = requests.get('https://api.spotify.com/v1/search?query=artist:lil&type=artist&market=us&limit=50&offset=' + str(page * 50))
    lil_data = lil_response.json()
    lil_artists = lil_data['artists']['items']
    for artist in lil_artists:
        total_lils = total_lils + 1
print('There are', total_lils, 'Lils on Spotify.')
print('It would take', round(total_lils / 12), 'minutes to request all of the Lils if you were requesting one every five seconds.')


There are 4500 Lils on Spotify.
It would take 375 minutes to request all of the Lils if you were requesting one every five seconds.

8) Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?


In [164]:
biggie_total_pop = 0
for artist in biggie_artists:
    biggie_total_pop = biggie_total_pop + artist['popularity']
biggie_avg_pop = biggie_total_pop / 50


3.84

In [171]:
lil_response_pg1 = requests.get('https://api.spotify.com/v1/search?query=artist:lil&type=artist&market=us&limit=50')
lil_data_pg1 = lil_response_pg1.json()
lil_artists_pg1 = lil_data_pg1['artists']['items']
lil_total_pop = 0
for artist in lil_artists_pg1:
    lil_total_pop = lil_total_pop + artist['popularity']
lil_avg_pop = lil_total_pop / 50


46.08

In [172]:
if biggie_avg_pop > lil_avg_pop:
    print('The top 50 biggies are more popular.')
elif biggie_avg_pop < lil_avg_pop:
    print('The top 50 lils are more popular.')
else:
    print('They are equally popular.')


The top 50 lils are more popular.

In [ ]: