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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:51:28+00:00 2026-05-27T11:51:28+00:00

I am performing shuffle operations on nested lists in Python 3. I wish to

  • 0

I am performing shuffle operations on nested lists in Python 3. I wish to reshuffle previously shuffled lists indefinitely until the order of the nested lists meet a specific criteria. random.shuffle operates in place and calling random.shuffle() on a previously shuffled list does not reshuffle it. What is the best way to reshuffle a list indefinitely until it meets a condition. For example, I was trying something like this, but making a new list then shuffling it doesn’t seem to work:

from random import shuffle

L1 = [[1,2], [3,4], [5,6], [7,8], [9,10]]
shuffle(L1)
match = L1[0]

# reshuffle until [9,10] is the first item in the list
if match != [9,10]:
    L1 = list(L1)
    shuffle(L1)
print(L1)
  • 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-27T11:51:28+00:00Added an answer on May 27, 2026 at 11:51 am

    This statement:

    calling random.shuffle() on a previously shuffled list does not reshuffle it

    This is incorrect. Observe:

    Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
    [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> L1 = [[1,2], [3,4], [5,6], [7,8], [9,10]]
    >>> from random import shuffle
    >>> shuffle(L1)
    >>> print L1
    [[3, 4], [9, 10], [5, 6], [7, 8], [1, 2]]
    >>> shuffle(L1)
    >>> print L1
    [[9, 10], [3, 4], [5, 6], [1, 2], [7, 8]]
    >>> 
    

    The following code should do what you want, although it will have a non-deterministic runtime.

    from random import shuffle
    
    L1 = [[1,2], [3,4], [5,6], [7,8], [9,10]]
    match = [9,10]
    while L1[0]!=match:
        shuffle(L1)
    print(L1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When performing many disk operations, does multithreading help, hinder, or make no difference? For
performing a GET in order to send data to an API before a user
I am performing some simulations in python that generates 1000 results. Each result has
When performing I/O operations in Java, is it generally more efficient (in terms of
After performing some operations I am getting a set of records. Example: a b
I am performing a stencil computation on a matrix I previously read from a
Right now I'm using the Mersenne Twister RNG and performing Fisher-Yates shuffle algorithm 100
When performing string concatentation of an existing string in the intern pool, is a
While performing a conditional DELETE operation in one of my InnoDB tables, which apparently
While performing some upcoming maintenance, I'm going to have to redirect all site traffic

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.