Python – List of N size increasing lists

Given integer N and M, construct increasing list till M, each list being N size i.e all combinations lists.

Input : N = 2, M = 3
Output : [(1, 2), (1, 3), (2, 3)]
Explanation : Increasing paired elements.

Input : N = 1, M = 4
Output : [(1, ), (2, ), (3, ), (4, )]
Explanation : Increasing paired elements.

Method #1 : Using loop

This is one brute force way in which this task can be performed. In this we iterate for elements till M, and form lists with N size. The drawback is that we are restricted to limited sized lists.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# List of N size increasing lists
# Using loop
  
# initializing N
N = 2
  
# initializing M
M = 4
  
# outer loop manages lists
res = []
for idx in range(1, M):
      
    # inner loop manages inner elements
    for j in range(idx + 1, M + 1):
        res.append((idx, j))
  
# printing result 
print("The constructed combinations : " + str(res)) 

chevron_right


Output

The constructed combinations : [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

Method #2 : Using combinations()

This is inbuild function offering exact functionality required for this solution. Solves the problem in one liner of getting increasing lists.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# List of N size increasing lists
# Using combinations()
from itertools import combinations
  
# initializing N
N = 2
  
# initializing M
M = 4
  
# using combinations to perform task 
res = list(combinations(list(range(1, M + 1)), N))
  
# printing result 
print("The constructed combinations : " + str(res)) 

chevron_right


Output

The constructed combinations : [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.