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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:32:16+00:00 2026-06-15T17:32:16+00:00

I am doing Project Euler Problem 15 . I used the right algorithm, but

  • 0

I am doing Project Euler Problem 15. I used the right algorithm, but it didn’t seem to work. Here’s my code:

f = [[0] * 21] * 21

# init the list
for i in range(21):
    f[0][i] = 1
    f[i][0] = 1

for i in range(21):
    for j in range(21):
        f[i][j] = f[i-1][j] + f[i][j-1]

print f[20][20]

when I finished initializing the list, I printed it. I expected it like [[1, 1, 1...], [1, 0, 0...]...], but it turned [[1, 1, 1...], [1, 1, 1...]...] and I can’t figure why.

I used to use C-like language and I thought the list in Python is kind of like the array in C, so I used them in the same way.

  • 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-15T17:32:17+00:00Added an answer on June 15, 2026 at 5:32 pm

    When multiplying a list, you are not creating separate lists but rather create multiple references to the same list.

    Instead, do this:

    f = [[0 for _ in range(21)] for _ in range(21)] 
    

    You can see the difference when using the id() function:

    >>> f = [[0]*21]*21
    >>> for nested in f[:3]:
    ...     print id(nested)
    ... 
    4523317152
    4523317152
    4523317152
    >>> f = [[0 for _ in range(21)] for _ in range(21)] 
    >>> for nested in f[:3]:
    ...     print id(nested)
    ... 
    4523317512
    4523317440
    4523317656
    

    Separate objects have separate memory id values, while your lists have the same result for each id() call, showing they are all the same list.

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

Sidebar

Related Questions

I'm doing problem 20 on Project Euler - finding the sum of the digits
I am doing problem 7 of Project Euler. What I am supposed to do
I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit
I am doing problem 61 at project Euler and came up with the following
I am doing problem 68 at project euler and came up with the following
I am doing problem 112 on Project Euler and came up with the following
I am doing project euler question 224 . And whipped up this list comprehension
I'm looking at Pavel's solution to Project Euler question 24, but can't quite work
So, I'm doing Project Euler to solidify my Ruby skills. I'm on problem #4,
I was trying to solve Project Euler problem 10 using python, but my program

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.