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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:19:41+00:00 2026-06-07T22:19:41+00:00

At the moment my code is as follows A = [matrix_x[i][:n] for i in

  • 0

At the moment my code is as follows

A = [matrix_x[i][:n] for i in xrange(0, n)]
B = [matrix_x[i][n:] for i in xrange(0, n)]
C = [matrix_x[i+n][:n] for i in xrange(0, n)]
D = [matrix_x[i+n][n:] for i in xrange(0, n)]

Is there a better way of doing this, since I am continually looping over the same xrange. In this instance, would if be better to not use a list comprehension and just append the values to each list while in a single for loop.

A,B,C,D = [],[],[],[]
for i in xrange(0,n):
    A.append(matrix_x[i][:n])
    B.append(matrix_x[i][n:])
    ... etc

Second way seems more efficient to me. What way would be more ‘pythonic’ or is there another way I haven’t thought of

  • 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-06-07T22:19:42+00:00Added an answer on June 7, 2026 at 10:19 pm

    I generally prefer to iterate over the list of items itself, rather than over xrange(len(list_of_items)) and working with the i’th item at a time. Here’s how to use zip to look at each (this,next) pair in a sequence, and then build up your lists:

    A,B,C,D = [],[],[],[]
    for this_,next_ in zip(matrix_x,matrix_x[1:]):
        A.append(this_[:n])
        B.append(this_[n:])
        C.append(next_[:n])
        D.append(next_[n:])
    

    And yes, you can really compact this down to a zip of a zip:

    A,B,C,D = zip(*((this_[:n],this_[n:],next_[:n],next_[n:])
                      for this_,next_ in zip(matrix_x,matrix_x[1:])))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At the moment I have this code which fires a projectile: Vector3 d =
Take this example code (ignore it being horribly inefficient for the moment) let listToString
Note: This code actually codes from a tutorial book I'm reading at the moment,
Hey I have my code as follows at the moment <script type=text/javascript> var activeTabId
I'm using the STL map data structure, and at the moment my code first
The following code summarizes the problem I have at the moment. My current execution
At the moment I have the following code which works fine. label = new
I have a code at the moment that takes a line from a .txt
Trying to write a code at the moment that basically tests to see if
I haven't got any code at the moment but I have a situation where

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.