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 6551735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:22:29+00:00 2026-05-25T12:22:29+00:00

I would like to extend a list while looping over it: for idx in

  • 0

I would like to extend a list while looping over it:

for idx in xrange(len(a_list)):
    item = a_list[idx]
    a_list.extend(fun(item))

(fun is a function that returns a list.)

Question:
Is this already the best way to do it, or is something nicer and more compact possible?

Remarks:

from matplotlib.cbook import flatten
a_list.extend(flatten(fun(item) for item in a_list))

should work but I do not want my code to depend on matplotlib.

for item in a_list:
    a_list.extend(fun(item))

would be nice enough for my taste but seems to cause an infinite loop.

Context:
I have have a large number of nodes (in a dict) and some of them are special because they are on the boundary.
‘a_list’ contains the keys of these special/boundary nodes. Sometimes nodes are added and then every new node that is on the boundary needs to be added to ‘a_list’. The new boundary nodes can be determined by the old boundary nodes (expresses here by ‘fun’) and every boundary node can add several new nodes.

  • 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-25T12:22:30+00:00Added an answer on May 25, 2026 at 12:22 pm

    Have you tried list comprehensions? This would work by creating a separate list in memory, then assigning it to your original list once the comprehension is complete. Basically its the same as your second example, but instead of importing a flattening function, it flattens it through stacked list comprehensions. [edit Matthias: changed + to +=]

    a_list += [x for lst in [fun(item) for item in a_list] for x in lst]  
    

    EDIT: To explain what going on.

    So the first thing that will happen is this part in the middle of the above code:

    [fun(item) for item in a_list]
    

    This will apply fun to every item in a_list and add it to a new list. Problem is, because fun(item) returns a list, now we have a list of lists. So we run a second (stacked) list comprehension to loop through all the lists in our new list that we just created in the original comprehension:

    for lst in [fun(item) for item in a_list]
    

    This will allow us to loop through all the lists in order. So then:

    [x for lst in [fun(item) for item in a_list] for x in lst]
    

    This means take every x (that is, every item) in every lst (all the lists we created in our original comprehension) and add it to a new list.

    Hope this is clearer. If not, I’m always willing to elaborate further.

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

Sidebar

Related Questions

I would like to require classes that extend my abstract class A to provide
I would like to manually extend the IntelliSense list by various items. I want
I would like to extend a kd-tree (2D) class to be able to remove
I would like to create extend a Java Swing application to have a look
How can I extend a builtin class in python? I would like to add
I'm trying to extend my ActiveRecord class with some dynamic methods. I would like
I would like to use Visual Studio 2008 to the greatest extent possible while
I have a custom-made view that extends the View class. I would like 2
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Is there a way to get a list of methods that would be accessible

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.