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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:00:10+00:00 2026-06-01T01:00:10+00:00

I have a problem with initialzing a 2D array in python. I want a

  • 0

I have a problem with initialzing a 2D array in python. I want a 6×6 array, I did

arr = [[None]*6]*6

But when I do:

>>> arr[1][2]=10
>>> arr
[[None, None, 10, None, None, None], [None, None, 10, None, None, None], [None, None, 10, None, None, None], [None, None, 10, None, None, None], [None, None, 10, None, None, None], [None, None, 10, None, None, None]]

Notice I just set 1 item, and its “replicated” on all rows. Whats wrong? I think it has to do with its referencing the same list, but how do I fix this?

I figured

for key, _ in algos.items():
    algoData[key] = []
    for i in range(0,6):
        algoData[key].append([])
        for j in range(0,6):
            algoData[key][i].append(None)

works, but it seems long to just initialize an empty 6×6 array, what if I want a 10000×10000 array, it will be very inefficient?

UPDATE

Can I also initialize a dictionary of 2D arrays? I have a dictionary like:

algos = { "FIFO": ..., "LRU": ..., "Random": ... }

I want to initialize a dictionary like below:

algoData = { "FIFO": 2D arr, "LRU": 2D arr, "Random": 2D arr }
  • 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-01T01:00:12+00:00Added an answer on June 1, 2026 at 1:00 am

    Using list comprehensions, you can say:

    arr = [[None for x in range(6)] for y in range(6)]
    

    Then you will have arr[1][2] = 10 working as expected. This is not a very normal thing to do, however. What are you going to use the nested lists for? There may be a better way. For example, working with arrays is made much easier with the numpy package.

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

Sidebar

Related Questions

I have a problem initializing an array whose size is defined as extern const.
I have problem with return statment >.< I want to store all magazine names
I have problem with fancybox. I want to write a function that will run
I have a seemingly basic problem with C# arrays. I am a beginner, but
actually I'm have a very simple problem (that's for sure) I just can't find
I have a problem that happens rarely enough but anyway could be caught. The
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for

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.