Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 793271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:07:28+00:00 2026-05-14T22:07:28+00:00

I have the following list comprehension which returns a list of coordinate objects for

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:07:28+00:00Added an answer on May 14, 2026 at 10:07 pm
    coordinate_list = [x for location in locations
                       for x in [Coordinates(location.latitude,
                                             location.longitude)
                                ] * location.number_of_times]
    

    Edit: the OP suggests a loop may be clearer, which, given the length of the identifiers, is definitely a possibility. The equivalent code would then be something like:

    coordinate_list = [ ]
    for location in locations:
        coord = Coordinates(location.latitude, location.longitude)
        coordinate_list.extend([coord] * location.number_of_times)
    

    The loop does look fine, partly because the extend method of lists works nicely here, and partly because you get to give a name to the Coordinate instance you’re extending with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following select list from which the user can select multiple values.
I have the following code which I use to map a nested list in
How to remove duplicate items from a list using list comprehension? I have following
Is filter/map equivalent to list comprehension? Suppose I have the following function def fib_gen():
I have the following List<int> collection and I need to find the highest integer
I have the following list <ul> <li id=item1>Item 1</li> <li id=item2>Item 2</li> <li>Item 3</li>
I have the following list in Python: [[1, 2], [3, 4], [4, 6], [2,
I have the following list of time zones that a user can choose from,
I have the following list: <s:List id=list x=4 y=4 width=99% height=99% dataProvider={recordingsShown} itemRenderer=components.VideoItemRenderer useVirtualLayout=false
If we have the following list: list = ['UMM', 'Uma', ['Ulaster','Ulter']] If I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.