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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:15:31+00:00 2026-05-10T15:15:31+00:00

How do I generate all the permutations of a list? For example: permutations([]) []

  • 0

How do I generate all the permutations of a list? For example:

permutations([]) []  permutations([1]) [1]  permutations([1, 2]) [1, 2] [2, 1]  permutations([1, 2, 3]) [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1] 
  • 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. 2026-05-10T15:15:32+00:00Added an answer on May 10, 2026 at 3:15 pm

    Use itertools.permutations from the standard library:

    import itertools list(itertools.permutations([1, 2, 3])) 

    Adapted from here is a demonstration of how itertools.permutations might be implemented:

    def permutations(elements):     if len(elements) <= 1:         yield elements         return     for perm in permutations(elements[1:]):         for i in range(len(elements)):             # nb elements[0:1] works in both string and list contexts             yield perm[:i] + elements[0:1] + perm[i:] 

    A couple of alternative approaches are listed in the documentation of itertools.permutations. Here’s one:

    def permutations(iterable, r=None):     # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC     # permutations(range(3)) --> 012 021 102 120 201 210     pool = tuple(iterable)     n = len(pool)     r = n if r is None else r     if r > n:         return     indices = range(n)     cycles = range(n, n-r, -1)     yield tuple(pool[i] for i in indices[:r])     while n:         for i in reversed(range(r)):             cycles[i] -= 1             if cycles[i] == 0:                 indices[i:] = indices[i+1:] + indices[i:i+1]                 cycles[i] = n - i             else:                 j = cycles[i]                 indices[i], indices[-j] = indices[-j], indices[i]                 yield tuple(pool[i] for i in indices[:r])                 break         else:             return 

    And another, based on itertools.product:

    def permutations(iterable, r=None):     pool = tuple(iterable)     n = len(pool)     r = n if r is None else r     for indices in product(range(n), repeat=r):         if len(set(indices)) == r:             yield tuple(pool[i] for i in indices) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 131k
  • Answers 131k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's not a good idea when you want your images… May 12, 2026 at 6:12 am
  • Editorial Team
    Editorial Team added an answer How about http://followcost.com/Anand_Dasgupta.json May 12, 2026 at 6:12 am
  • Editorial Team
    Editorial Team added an answer In my experience, SVN prompts for authentication whenever required. Meaning… May 12, 2026 at 6:12 am

Related Questions

I have a list of objects (for the sake of example, let's say 5).
Given this input: [1,2,3,4] I'd like to generate the set of spanning sets: [1]
I have a page that displays two objects and then the user picks one
Say I have an array that represents a set of points: x = [2,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.