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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:01:16+00:00 2026-06-14T13:01:16+00:00

In Python I want an intuitive way to create a 3 dimensional list. I

  • 0

In Python I want an intuitive way to create a 3 dimensional list.

I want an (n by n) list. So for n = 4 it should be:

x = [[[],[],[],[]],[[],[],[],[]],[[],[],[],[]],[[],[],[],[]]]

I’ve tried using:

y = [n*[n*[]]]    
y = [[[]]* n for i in range(n)]

Which both appear to be creating copies of a reference.
I’ve also tried naive application of the list builder with little success:

y = [[[]* n for i in range(n)]* n for i in range(n)]
y = [[[]* n for i in range(1)]* n for i in range(n)]

I’ve also tried building up the array iteratively using loops, with no success. I also tried this:

y = []
for i in range(0,n):
    y.append([[]*n for i in range(n)])

Is there an easier or more intuitive way of doing this?

  • 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-14T13:01:18+00:00Added an answer on June 14, 2026 at 1:01 pm

    I think your list comprehension versions were very close to working. You don’t need to do any list multiplication (which doesn’t work with empty lists anyway). Here’s a working version:

    >>> y = [[[] for i in range(n)] for i in range(n)]
    >>> print y
    [[[], [], [], []], [[], [], [], []], [[], [], [], []], [[], [], [], []]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Python I want to create a symbolic link pointing to a path that
is there any way to update multiple lines of a textfile using python.I want
Using Python I want to create a property in a class, but having the
Using Python I want to randomly rearrange sections of a string based on a
Possible Duplicate: How to generate all permutations of a list in Python I want
I'm trying to create a espeak based talking clock completely in python I want
I want a python function that takes a pdf and returns a list of
I'm using python, and I have a list of sets, constructed like this: list
Using Python I want to replace all URLs in a body of text with
I'm using python and want to embed gtk3 widgets in clutter stage. However, it

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.