So I have a list of words and numbers, and i was trying to use a for loop and range function to remove the numbers so that I remain with only the words, as shown in the format below;
# This is a sample of elements in the list:
billboard_artists = \['The Chainsmokers Featuring Halsey', '6', '1', '3', '6', '1', '3', 'Sia Featuring Sean Paul', '1', '1', '27', '1', '1', '27', 'Major Lazer Featuring Justin Bieber & MO', '2', '2', '4', '2', '2', '4', 'twenty one pilots', '4', '4', '9', '4', '4', '9', 'Calvin Harris Featuring Rihanna'\]
for item in billboard_artists:
try:
for num in range(100):
if int(item) == num:
billboard_artists.remove(item)
except ValueError:
print(item)
print(billboard_artists)
The expected result was to get a full list containing just the songs. However, the loop only works for the first 56 elements or so, before a few of the numbers start re-appearing again:
# ['The Chainsmokers Featuring Halsey', 'Sia Featuring Sean Paul', 'Major Lazer Featuring Justin Bieber & MO', 'twenty one pilots', 'Calvin Harris Featuring Rihanna', 'twenty one pilots', 'Drake Featuring WizKid & Kyla', 'The Chainsmokers Featuring Daya', '3', 'Justin Timberlake', '1', '1', 'Adele', 'Rihanna']
The names on the list are to be used in making a playlist using the Spotify Restful API.
After noticing this issue, I went through a different solution using the ValueError exception to make a different list of the artists.
my_list = []
for item in billboard_artists:
try:
for num in range(100):
if int(item) == num:
billboard_artists.remove(item)
except ValueError:
my_list.append(item)
Which DID eventually return the full list of only the words and not the numbers. (Mission Accomplished I guess)
However, it still bothers me WHY the RANGE function BROKE DOWN AFTER A FEW ITERATIONS(60 or so). Any possible explanations will be greatly appreciated by the author.
CodePudding user response:
I think there are a number of reasons that could impact your output. One being that the size of your list is changing as your iterate over it (you are removing items from the list while you are going over it). But it seems like since your second implementation worked this isn't a huge issue in this case.
The other reason I can think of is that in your list item
would for example be "1"
. There are many instances of "1" in your list, and the official docs say that list.remove()
removes the first item in the list that it encounters with that value. But if the size of the list is changing as you are trying to remove this could skip over some items.
Lastly, this is not the best way you could have achieved this, for each item in the list, you are iterating over another list 100 times, and then on top of that list.remove()
iterates over the entire list each time as well - making it very inefficient. You don't really need to worry about this, you can rewrite your function as follows:
my_list = [item for item in billboard_artists if not item.isnumeric()]
in this implementation, you are not removing anything from the billboard_artists
list so you won't have any wonky behvaiour! And it only goes over the billboards_artists
list once :)