Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Incremental slice partition in list

Improve Article
Save Article
  • Last Updated : 18 Jan, 2023
Improve Article
Save Article

Sometimes, while working with lists, we can come across a problem in which we need to slice a list incrementally, i.e, with each slice, number of elements increase by 1. This has application in competitive programming. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using loops This is the brute force method by which this task can be performed. We just manually count and increase the counter with each iteration for slicing and dictionary key creation. 

Python3




# Python3 code to demonstrate working of
# Incremental slice partition in list
# Using loop
 
# initializing list
test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Incremental slice partition in list
# Using loop
res = {}
N = 1
strt = 0
while strt < len(test_list):
    res[N] = test_list[strt : strt + N]
    strt += N
    N += 1
 
# printing result
print("The partitioned dictionary from list is : " + str(res))

Output : 

The original list is : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] The partitioned dictionary from list is : {1: [1], 2: [2, 3], 3: [4, 5, 6], 4: [7, 8, 9, 10]}

  Method #2 : Using enumerate() + slice() + next() + iter() + count() The combination of above functions can be used to perform this task. In this, next() is used to iterate the list converted to iterator by iter(). The slice() performs list slicing. The count() helps in managing the counter and enumerate keeps track of element and indices in list. 

Python3




# Python3 code to demonstrate working of
# Incremental slice partition in list
# Using enumerate() + slice() + next() + iter() + count()
from itertools import islice, count
 
# initializing list
test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Incremental slice partition in list
# Using enumerate() + slice() + next() + iter() + count()
res = {key : val for key, val in enumerate(iter(lambda i = iter(test_list),
                          c = count(1): list(islice(i, next(c))), []), 1)}
 
# printing result
print("The partitioned dictionary from list is : " + str(res))

Output : 

The original list is : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] The partitioned dictionary from list is : {1: [1], 2: [2, 3], 3: [4, 5, 6], 4: [7, 8, 9, 10]}

Using recursion:

Python3




def recursive_slice(lst, start, step, res):
    # base case: if start index exceeds the length of the list, return the result
    if start >= len(lst):
        return res
    # slice the list and store it in the result dictionary
    res[step] = lst[start : start + step]
    # recursively call the function with updated start and step index
    return recursive_slice(lst, start + step, step + 1, res)
 
# initial test list
test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# empty dictionary to store the result
result = {}
# call the recursive function and print the result
print(recursive_slice(test_list, 0, 1, result))
 
 
#This code is contributed by Edula Vinay Kumar Reddy

Output

{1: [1], 2: [2, 3], 3: [4, 5, 6], 4: [7, 8, 9, 10]}

In the above example, a recursive function recursive_slice is defined which takes four arguments lst, start, step and res. In each iteration, it slices the given list from start to start + step and stores the sliced portion in a dictionary res with key as step and value as the sliced portion of the list. This function calls itself with updated parameters of start and step and res until the value of start becomes greater than or equal to the length of the list.

Time and space complexity of this method will be O(n) as we are iterating through the whole list and creating a new dictionary with n key-value pairs.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!