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

  • Home
  • SEARCH
  • 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 7164263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:02:46+00:00 2026-05-28T14:02:46+00:00

Basically, I need to get few permutations of a list. So, the method that

  • 0

Basically, I need to get few permutations of a list. So, the method that I have used is to shuffle the list string randomly to get the permutation and add it to a list, while adding I check if there exists the same permutation in the list. I am not able to implement the check. Here is the code that I have written.

list = [x for x in range(0,max)]
totalperm = 10
perms = []
while(len(perms) <> totalperm):
    random.shuffle(list)        
    if list not in perms:
        perms.append(list)            

Please let me know what am I missing here.

  • 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-28T14:02:47+00:00Added an answer on May 28, 2026 at 2:02 pm

    This doesn’t work because you’re working with a reference to the single list the whole time, so it is always in the list after the first time you append it.

    Consider:

    perms = []
    items = [1, 2, 3]
    random.shuffle(mylist)   # perhaps items is now [2, 1, 3]
    perms.append(items)      # perms is now [[2, 1, 3]]
    random.shuffle(mylist)   # perhaps items is now [1, 3, 2]
    perms.append(items)      # now what is perms?
    

    The answer is, perms has two references to a single list, so it is [1, 3, 2], [1, 3, 2]].

    In your code, items and perms[0] are the same object and have the same value. So when you ask if items is in perms the answer is always yes. Because you just put it in there!

    The solution is simple: add a copy of the list.

    perms.append(items[:])
    

    By the way, you should probably use a set rather than a list for storing your permutations. It’s much faster to check membership of a set and attempts to add duplicates are simply ignored. The only downside is that you can’t put lists in a set because lists are mutable and therefore unhashable — fortunately, you can just convert them to tuples, which are immutable and hashable. Then you have:

    items = range(0, max)    # Python 3: items = list(range(0, max))
    perms = set()
    nump  = 10
    while len(perms) < nump:
        random.shuffle(items)
        perms.add(tuple(items)) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few tables that I need to link together to get a
I have an html-like xml, basically it is html. I need to get the
I need a regular expression to basically get the first part of a string,
I have an ASP website and I need to add a form that will
basically need to change the value that - admin_url() returns any idea?
I have a few lookup tables that I am in the process of plumbing
I have a few functions that I'm using to draw different type of text.
I'm taking a cloud computing course and have a few problems that I am
I am entering a realm that is new to me, but basically I need
Basically, i need a file of specified format and large size(Around 10gb). To get

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.