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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:34:58+00:00 2026-05-27T03:34:58+00:00

I have a string. I want to generate all permutations from that string, by

  • 0

I have a string. I want to generate all permutations from that string, by changing the order of characters in it. For example, say:

x='stack'

what I want is a list like this,

l=['stack','satck','sackt'.......]

Currently I am iterating on the list cast of the string, picking 2 letters randomly and transposing them to form a new string, and adding it to set cast of l. Based on the length of the string, I am calculating the number of permutations possible and continuing iterations till set size reaches the limit.
There must be a better way to do this.

  • 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-27T03:34:59+00:00Added an answer on May 27, 2026 at 3:34 am

    The itertools module has a useful method called permutations(). The documentation says:

    itertools.permutations(iterable[, r])

    Return successive r length permutations of elements in the iterable.

    If r is not specified or is None, then r defaults to the length of the
    iterable and all possible full-length permutations are generated.

    Permutations are emitted in lexicographic sort order. So, if the input
    iterable is sorted, the permutation tuples will be produced in sorted
    order.

    You’ll have to join your permuted letters as strings though.

    >>> from itertools import permutations
    >>> perms = [''.join(p) for p in permutations('stack')]
    >>> perms
    

    [‘stack’, ‘stakc’, ‘stcak’, ‘stcka’, ‘stkac’, ‘stkca’, ‘satck’,
    ‘satkc’, ‘sactk’, ‘sackt’, ‘saktc’, ‘sakct’, ‘sctak’, ‘sctka’,
    ‘scatk’, ‘scakt’, ‘sckta’, ‘sckat’, ‘sktac’, ‘sktca’, ‘skatc’,
    ‘skact’, ‘skcta’, ‘skcat’, ‘tsack’, ‘tsakc’, ‘tscak’, ‘tscka’,
    ‘tskac’, ‘tskca’, ‘tasck’, ‘taskc’, ‘tacsk’, ‘tacks’, ‘taksc’,
    ‘takcs’, ‘tcsak’, ‘tcska’, ‘tcask’, ‘tcaks’, ‘tcksa’, ‘tckas’,
    ‘tksac’, ‘tksca’, ‘tkasc’, ‘tkacs’, ‘tkcsa’, ‘tkcas’, ‘astck’,
    ‘astkc’, ‘asctk’, ‘asckt’, ‘asktc’, ‘askct’, ‘atsck’, ‘atskc’,
    ‘atcsk’, ‘atcks’, ‘atksc’, ‘atkcs’, ‘acstk’, ‘acskt’, ‘actsk’,
    ‘actks’, ‘ackst’, ‘ackts’, ‘akstc’, ‘aksct’, ‘aktsc’, ‘aktcs’,
    ‘akcst’, ‘akcts’, ‘cstak’, ‘cstka’, ‘csatk’, ‘csakt’, ‘cskta’,
    ‘cskat’, ‘ctsak’, ‘ctska’, ‘ctask’, ‘ctaks’, ‘ctksa’, ‘ctkas’,
    ‘castk’, ‘caskt’, ‘catsk’, ‘catks’, ‘cakst’, ‘cakts’, ‘cksta’,
    ‘cksat’, ‘cktsa’, ‘cktas’, ‘ckast’, ‘ckats’, ‘kstac’, ‘kstca’,
    ‘ksatc’, ‘ksact’, ‘kscta’, ‘kscat’, ‘ktsac’, ‘ktsca’, ‘ktasc’,
    ‘ktacs’, ‘ktcsa’, ‘ktcas’, ‘kastc’, ‘kasct’, ‘katsc’, ‘katcs’,
    ‘kacst’, ‘kacts’, ‘kcsta’, ‘kcsat’, ‘kctsa’, ‘kctas’, ‘kcast’,
    ‘kcats’]

    If you find yourself troubled by duplicates, try fitting your data into a structure with no duplicates like a set:

    >>> perms = [''.join(p) for p in permutations('stacks')]
    >>> len(perms)
    720
    >>> len(set(perms))
    360
    

    Thanks to @pst for pointing out that this is not what we’d traditionally think of as a type cast, but more of a call to the set() constructor.

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

Sidebar

Related Questions

So I have a string SmartUserWantsToLive I want to generate from it or any
I have a list of strings. From this list i want to generate a
i have a simple array and i want to generate string which include all
I have string that I want to chop to array of substrings of given
I have a HTML string and want to replace all links to just a
I have a string that I want passed via the linebreaks filter. {% trans
I want to auto-generate a unique 8-10 character ID string that includes a checksum
So i have a regex pattern, and I want to generate all the text
Using XSLT and XPath 1.0, I have a string I want to escape for
I have a string pc1|pc2|pc3| I want to get each word on different line

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.