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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:56:43+00:00 2026-06-18T20:56:43+00:00

Consider all lists of length n where the values are integers from the range

  • 0

Consider all lists of length n where the values are integers from the range 0 to n-1. I would like to iterate as quickly as possible over every possible such list that has exactly one duplicate. If n = 2 then the possible lists are:

00
11

If n = 3 they are:

001
002
110
112
220
221
100
200
011
211
022
122
010
020
101
121
202
212

The total number of such lists is n! * (n choose 2) so I would like to avoid storing them all in memory if at all possible.

For each list I want to call a function that calculates a function of the list.
What’s a good way to do this?

Benchmarks On my computer I get the following benchmark results

timeit all(one_duplicate(6))
100 loops, best of 3: 6.87 ms per loops
timeit all(one_duplicate(7))
10 loops, best of 3: 59 ms per loop
timeit all(one_duplicate(8))
1 loops, best of 3: 690 ms per loop
timeit all(one_duplicate(9))
1 loops, best of 3: 7.58 s per loop

and

timeit all(duplicates(range(6)))
10 loops, best of 3: 22.8 ms per loop
timeit all(duplicates(range(7)))
1 loops, best of 3: 416 ms per loop    
timeit all(duplicates(range(8)))
1 loops, best of 3: 9.78 s per loop

and

timeit all(all_doublet_tuples(6))
100 loops, best of 3: 6.36 ms per loop
timeit all(all_doublet_tuples(7))
10 loops, best of 3: 60 ms per loop
timeit all(all_doublet_tuples(8))
1 loops, best of 3: 542 ms per loop
timeit all(all_doublet_tuples(9))
1 loops, best of 3: 7.27 s per loop

I declare all_doublet_tuples and one_duplicate to be equal first at the moment (allowing for noise in the tests).

  • 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-06-18T20:56:45+00:00Added an answer on June 18, 2026 at 8:56 pm

    Think of any of the desired output tuples in this way: It was derived by duplicating the first (lets say from the left) doublet elem and inserting it at the position of the second doublet.

    Therefore my solution is basically this two-step process:

    1. generate all n-1 permutations of range(n)
    2. for each tuple from 1.:
      take each single element and insert it at each position ahead (to avoid duplicates) including at the very end

    import itertools as it
    
    # add_doublet accomplishes step 2
    def add_doublet(t):
        for i in range(len(t)):
            for j in range(i+1, len(t)+1):
                yield t[:j] + (t[i],) + t[j:]
    
    
    def all_doublet_tuples(n):
        for unique_tuple in it.permutations(range(n), n-1):
            for doublet_tuple in add_doublet(unique_tuple):
                yield doublet_tuple
    
    
    
    from pprint import pprint
    
    n = 3
    pprint(list(all_doublet_tuples(n)))
    

    I do not print the output here cause its kind of lenghty … and the case for n=3 is boring.

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

Sidebar

Related Questions

I have several lists, that you can consider as rows of integers. For example
Possible Duplicate: sorted() using Generator Expressions Rather Than Lists We all know using generators
Consider there are some lists of integers as: #-------------------------------------- 0 [0,1,3] 1 [1,0,3,4,5,10,...] 2
Consider the following VBScript which, when run, lists all the files in the current
Consider following make: all: a b a: echo a exit 1 b: echo b
Consider the following script: for host in $(get-all-hosts) do (restart-server $host; wait-for-server-to-come-up $host) &
Consider a Console application that starts up some services in a separate thread. All
I have a table that lists people and all their contact info. I want
Consider a class like this one: class MyReferenceClass { public: MyReferenceClass(); const double ImportantConstant1;
What would anyone consider the most efficient way to merge two datasets using Python?

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.