Python: Repeat elements in a list comprehension?
- by User
I have the following list comprehension which returns a list of coordinate objects for each location.
coordinate_list = [Coordinates(location.latitude, location.longitude)
for location in locations]
This works.
Now suppose the location object has a number_of_times member. I want a list comprehension to generate n Coordinate objects where n is the number_of_times for the particular location. So if a location has number_of_times = 5 then the coordinates for that location will be repeated 5 times in the list. (Maybe this is a case for a for-loop but I'm curious if it can be done via list comprehensions)