Python | Subgroups of i’th index size in list

Sometimes we need to group elements and grouping techniques and requirements vary accordingly. One such way to group the elements is by the i’th size in list which stores the dictionary of index keys with values as list elements of subsequent size i.

Input : [4, 7, 8, 10, 12, 15, 13, 17, 14, 5]
Output: {1: [4], 2: [7, 8], 3: [10, 12, 15], 4: [13, 17, 14, 5]}

Let’s discuss certain ways in which this can be done.



Method #1 : Using islice() + dictionary comprehension
The slice method can be used to group the chunks of list required to be made as values of the dictionaries which are then assigned to their destined index key using the dictionary comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Subgrouping of i'th index size in list
# using islice() + dictionary comprehension
from itertools import islice
  
# initializing list
test_list = [4, 7, 8, 10, 12, 15, 13, 17, 14, 5]
  
# printing original list 
print("The original list : " + str(test_list))
  
# using islice() + dictionary comprehension
# Subgrouping of i'th index size in list
temp = iter(test_list)
res = {key: val for key, val in ((i, list(islice(temp, i)))
                for i in range(1, len(test_list))) if val}
  
# printing result
print("The grouped dictionary is : " + str(res))

chevron_right


Output :

The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14, 5]
The grouped dictionary is : {1: [4], 2: [7, 8], 3: [10, 12, 15], 4: [13, 17, 14, 5]}

 

Method #2 : Using itemgetter() + takewhile() + islice()
In order to increase the computation speed, we introduce new functions to perform this particular task, takewhile and itemgetter functions which performs the task of grouping the sliced values.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Subgrouping of i'th index size in list
# using itemgetter() + takewhile() + islice()
from itertools import islice, takewhile
from operator import itemgetter
  
# initializing list
test_list = [4, 7, 8, 10, 12, 15, 13, 17, 14, 5]
  
# printing original list 
print("The original list : " + str(test_list))
  
# using itemgetter() + takewhile() + islice()
# Subgrouping of i'th index size in list
temp = iter(test_list)
res =  {key: val for key, val in 
        takewhile(itemgetter(1), ((i, list(islice(temp, i)))
        for i in range(1, len(test_list))))}
  
# printing result
print("The grouped dictionary is : " + str(res))

chevron_right


Output :

The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14, 5]
The grouped dictionary is : {1: [4], 2: [7, 8], 3: [10, 12, 15], 4: [13, 17, 14, 5]}



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.