Python: Repeat elements in a list comprehension?

Posted by User on Stack Overflow See other posts from Stack Overflow or by User
Published on 2010-05-17T03:43:20Z Indexed on 2010/05/17 3:50 UTC
Read the original article Hit count: 260

Filed under:
|

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)

© Stack Overflow or respective owner

Related posts about python

Related posts about list-comprehension