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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:08:43+00:00 2026-06-04T04:08:43+00:00

Hey guys I’m using python and I was just wondering how we can make

  • 0

Hey guys I’m using python and I was just wondering how we can make combinations of lists of specific lengths from an original list of elements? For example, I have an original list ["2H", "AH", "KH", "QH", "JH", "0H", "9H"], is there any way for me to create a final list where ALL of the new assorted lists of specific lengths (3 to 5) are contained?

Basically from the original list above, I want a returned list as shown below:

[['9H', '0H', 'JH'], ['0H', 'JH', 'QH'], ['JH', 'QH', 'KH'], ['QH', 'KH', 'AH'], 
['KH', 'AH', '2H'], ['9H', '0H', 'JH', 'QH'], ['0H', 'JH', 'QH', 'KH'], 
['JH', 'QH', 'KH', 'AH'], ['QH', 'KH', 'AH', '2H'], ['9H', '0H', 'JH', 'QH', 'KH'], 
['0H', 'JH', 'QH', 'KH', 'AH'], ['JH', 'QH', 'KH', 'AH', '2H']]

Thanks!

  • 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-04T04:08:45+00:00Added an answer on June 4, 2026 at 4:08 am
    >>> from itertools import islice, chain
    >>> L = ["2H", "AH", "KH", "QH", "JH", "0H", "9H"]
    >>> list(chain.from_iterable(
            zip(*[islice(reversed(L),i,None) for i in range(j)])
            for j in range(3,6)))
    [('9H', '0H', 'JH'), ('0H', 'JH', 'QH'), ('JH', 'QH', 'KH'), ('QH', 'KH', 'AH'), 
     ('KH', 'AH', '2H'), ('9H', '0H', 'JH', 'QH'), ('0H', 'JH', 'QH', 'KH'), 
     ('JH', 'QH', 'KH', 'AH'), ('QH', 'KH', 'AH', '2H'), ('9H', '0H', 'JH', 'QH', 'KH'),
     ('0H', 'JH', 'QH', 'KH', 'AH'), ('JH', 'QH', 'KH', 'AH', '2H')]
    

    Explanation

    This solution builds on the use of zip.

    >>> zip(L,L)
    [('2H', '2H'), ('AH', 'AH'), ('KH', 'KH'), ('QH', 'QH'), ('JH', 'JH'), ('0H', '0H'), ('9H', '9H')]
    

    This shows an example of how zip works, by taking an item from each of its paramters together.

    >>> zip(L,L[1:])
    [('2H', 'AH'), ('AH', 'KH'), ('KH', 'QH'), ('QH', 'JH'), ('JH', '0H'), ('0H', '9H')]
    

    By starting at the 1st item of the same list as the second paramater you can get every 2 items.

    >>> zip(L,L[1:],L[2:])
    [('2H', 'AH', 'KH'), ('AH', 'KH', 'QH'), ('KH', 'QH', 'JH'), ('QH', 'JH', '0H'), ('JH', '0H', '9H')]
    

    Same for every 3 items.

    >>> zip(*[L[i:] for i in range(3)])
    [('2H', 'AH', 'KH'), ('AH', 'KH', 'QH'), ('KH', 'QH', 'JH'), ('QH', 'JH', '0H'), ('JH', '0H', '9H')]
    

    To do this automatically you can use the * (splat) to get the arguments of zip from a list comprehension.

    In the example, the results come in reverse, so reversed gives a reverse iterator through the list. To slice an iterator, islice from itertools must be used. islice takes the iterable followed by the index to start the slice and the index to end it. None can be used to go all the way to the end of the iterable.

    >>> zip(*[islice(reversed(L),i,None) for i in range(3)])
    [('9H', '0H', 'JH'), ('0H', 'JH', 'QH'), ('JH', 'QH', 'KH'), ('QH', 'KH', 'AH'), ('KH', 'AH', '2H')]
    

    So that gives all the results for every 3 items. Now every 4 and 5 items are needed so this operation is performed for range(3), range(4) and range(5).

    (zip(*[islice(reversed(L),i,None) for i in range(j)]) for j in range(3,6))
    

    This would give a generator with a list for each of 3, 4, and 5 but they need to be all one list. They can be chained together withchain.from_iterable with the result being converted to a list.

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

Sidebar

Related Questions

Hey guys I just noticed that my join statement here SELECT * FROM reports
Hey guys I can't seem to get the syntax here right, I'm just trying
Hey guys so I'm trying and get the n number of lists from a
hey guys, i was using Json dll in my project from quite sometime now
Hey guys I was wondering how I could make the title of my website
Hey Guys, here is my code for this, the only help i get from
Hey guys, I'm using Twitter's PHP API, called twitterlibphp , and it works well,
Hey guys I was wondering if someone could provide a little help. I've been
Hey guys I was wondering if there is a way to get a certain
hey guys how to make these php html links codes possible. i have tried

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.