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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:30:17+00:00 2026-06-09T10:30:17+00:00

As I wrote a bit of code tonight, I came across a problem that

  • 0

As I wrote a bit of code tonight, I came across a problem that even though it doesn’t stop me, caught my attention for I couldn’t figure it out. So here’s a function I wrote (not supposed to be the optimal way to do it but nevermind…)

def ownShuffle( origin ):
    export  = [[] for i in range( len( origin ) ) ]
    indices = range( len( origin ) )
    for n, item in enumerate( origin ):
        i = random.randrange( len( indices ) )
        export[indices[i]] = item
        indices.remove(indices[i])
    return export

Now with a test sample like this:

c = [[1, 2, 3], 
     [4, 5, 6], 
     [7, 8, 9]]

There is the problem part. I get different results with the almost same code.
If it write this:

for i, line in enumerate(c):
    c[i] = ownShuffle(line)

print c
>>> [[3, 2, 1],
     [6, 4, 5],
     [7, 8, 9]]

I get a shuffled list. But with the following code:

for i, line in enumerate(c):
    line = ownShuffle(line)

print c
>>> [[1, 2, 3], 
     [4, 5, 6], 
     [7, 8, 9]]

I get the test sample unchanged. Perhaps it comes from the function I wrote? I have no idea…

So there goes my question: Does anyone know why?

Thank you 🙂

  • 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-09T10:30:19+00:00Added an answer on June 9, 2026 at 10:30 am
    for i, line in enumerate(c):
        line = ownShuffle(line)
    

    You keep creating a temporary line but its not the same reference as your original list c. You would actually need to affect the values inside of line for it to reflect the original object.

    You could see it changing the values inside of that same line by doing:

    for i, line in enumerate(c):
        line[:] = ownShuffle(line)
    

    Here is a way to visualize what is happening:

    for i, line in enumerate(c):
        id_before = id(line)
        line = ownShuffle(line)
        print id_before, "=>", id(line)
    # 4973032728 => 4973032656
    # 4973034312 => 4973032656
    # 4973034240 => 4973032656
    
    for i, line in enumerate(c):
        id_before = id(line)
        line[:] = ownShuffle(line)
        print id_before, "=>", id(line)
    # 4973032728 => 4973032728
    # 4973034312 => 4973034312
    # 4973034240 => 4973034240
    

    In the first one you can see that its always a temporary object. Whereas in the second, the line is still the original line element from your c

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

Sidebar

Related Questions

I have a bit of code I wrote, that echo's 3 variable (quiz name,
I tried to play a bit with node.js and wrote following code (it doesn't
I've got the following bit of code (that another developer wrote) that I need
i have a bit of code that i wrote a few weeks ago (the
I wrote a short bit of code to simply skip num_lines lines in an
The machine I use is 64-bit I wrote assembly code like this: movl $0x12e48ead
I noticed people wrote about this circular reference problem quite a bit before but
I just wrote perhaps the ugliest bit of MVC code in existence. It is:
So I wrote this little bit of code to try out some new way
I wrote a short bit of WatiN code (see below). it works great in

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.