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

  • Home
  • SEARCH
  • 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 4069074
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:27:18+00:00 2026-05-20T16:27:18+00:00

I am creating a list of lists using this code: zeroArray = [0]*Np zeroMatrix

  • 0

I am creating a list of lists using this code:

zeroArray = [0]*Np
zeroMatrix = []
for i in range(Np):
    zeroMatrix.append(zeroArray[:])

Is there a more efficient way to do this? I’m hoping for something along the lines of zeroArray = [0]*Np; zeroMat = zeroArray*Np but can’t find anything similar.

  • 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-20T16:27:19+00:00Added an answer on May 20, 2026 at 4:27 pm

    You could do this:

    zeroMatrix = [[0] * Np for i in range(Np)]
    

    Update: Well if we’re going to make it into a race, I’ve found something faster (on my computer) than Omnifarious’ method. This doesn’t beat numpy of course; but this is all academic anyway right? I mean we’re talking about microseconds here.

    I think this works because it avoids append and avoids preallocating zeroMatrix.

    zeroArray = [0] * Np
    zeroMatrix = [zeroArray[:] for i in range(Np)]
    

    My test results:

    $ python -m timeit -s "Np = 80" "zeroMatrix = [[0] * Np for i in range(Np)]"
    1000 loops, best of 3: 200 usec per loop
    $ python -m timeit -s "Np = 80" "zeroArray = [0] * Np" "zeroMatrix = [None] * Np" "for i in range(Np):" "    zeroMatrix[i] = zeroArray[:]"
    10000 loops, best of 3: 171 usec per loop
    $ python -m timeit -s "Np = 80" "zeroArray = [0] * Np" "zeroMatrix = [zeroArray[:] for i in range(Np)]"
    10000 loops, best of 3: 165 usec per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way of creating an alphabetically sorted list in Python?
What would anyone consider the most efficient way to merge two datasets using Python?
using c# (asp.net) i'm programmatically creating several drop down lists (random number). the user
I'm creating an options dialog using WPF that lists possible keys so that the
I am having problems creating WCF client proxy for service code like in this
I am creating a number of dropdown lists dynamically using jQuery. I would like
I have this Code of Showing all Records to a Message box using string
I'm creating a custom drop down list with AJAX dropdownextender. Inside my drop panel
Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups.
Creating Traversals for Binary Search Tree with Recursion. void inOrder(void (*inOrderPtr)(T&)) { if(this->left !=

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.