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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:27:46+00:00 2026-05-15T22:27:46+00:00

Started to learn programming 2 months ago, in one of my little projects i

  • 0

Started to learn programming 2 months ago, in one of my little projects i encountered the need to generate permutations of list of objects.
I knew that i’ll find how to do this if i’ll just searched for it, but i wanted to make one of my own, so i worked up and made my own permutation generator code:

def perm(lst, c = [], x = 0):
    i = -1
    g = len(lst) - 1
    if x == g:
        while i < g:
            i += 1
            if lst[i] in c:
                continue
            c.append(lst[i])
            print(c)
            del c[-1]
            i = g
    else:
        while i < g:
            if x == 0:
                del c[:]
            elif g == x:
                del c[-1]
            elif len(c) > x:
                del c[-1]
                continue
            i += 1
            if lst[i] in c:
                continue
            c.append(lst[i])
            x + 1
            perm(lst, c, x + 1)

This is what it gives if i run it:

perm(range(2))
[0, 1]
[1, 0]

perm([1, 4, 5])
[1, 4, 5]
[1, 5, 4]
[4, 1, 5]
[4, 5, 1]
[5, 1, 4]
[5, 4, 1]

It works as i expected, but when i use bigger lists it take some time for it to generate all the permutations of the list.
So all i want is hints on how to improve my code, only hints.
Or if you can tell me what should i learn to be able to make a better generator?

Thanks in advance for all the helpers.

  • 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-15T22:27:47+00:00Added an answer on May 15, 2026 at 10:27 pm

    The best way to understand what is making your code slow is to actually measure it. When you attempt to guess at what will make something fast, it’s often wrong. You’ve got the right idea in that you’re noticing that your code is slower and it’s time for some improvement.

    Since this is a fairly small piece of code, the timeit module will probably be useful. Break the code up into sections, and time them. A good rule of thumb is that it’s better to look at an inner loop for improvements, since this will be executed the most times. In your example, this would be the loop inside the perm function.

    It is also worth noting that while loops are generally slower than for loops in python, and that list comprehensions are faster than both of these.

    Once you start writing longer scripts, you’ll want to be aware of the ability to profile in python, which will help you identify where your code is slow. Hopefully this has given you a few places to look.

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

Sidebar

Related Questions

I started using subversion for one of my projects and it would be absolutely
I just started to learn Ruby and as a .Net developer, I'm wondering if
I just have started to learn Haskell and combine reading books and tutorials with
I've recently started to learn python , and I reached the with statement .
I recently started to learn about LINQ and came across the OrderBy extension method.
I've just started to learn about tie . I have a class named Link
I am extremely new to python, having started to learn it less than a
I started trying to play with Mono, mostly for fun at the moment. I
I started an application in Google App Engine right when it came out, to
I started a Rails project recently and decided to use RESTful controllers. I created

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.