Reputation: 12519
I have a list of 100 URLs in a list called recipe_urls
and I am trying to remove the the first 37 characters from each element in that list and store it in a new list called recipe_names
. How can I increment the index position in .insert(). Maybe I am thinking about this wrong and there is an easier way to do this?
recipe_names = []
for url in recipe_urls:
recipe_names.insert(x, url[37:0])
Upvotes: 0
Views: 169
Reputation: 35069
Since you always want it to go at the end of recipe_names
, you can use the index -1, or even just call append
instead (which doesn't take an index, and always puts the new value at the end of the list). But whenever you have code that creates an empty list, then iterates over something else to append to that new list, you can use a list comprehension instead:
recipe_names = [url[37:] for url in recipe_urls]
Upvotes: 1
Reputation: 12092
This is all you need:
recipe_names = [x[37:] for x in recipe_urls]
This method of building a list from another iterable or sequence is called list comprehension
Upvotes: 1