Python | Incremental slice partition in list
Last Updated :
26 Apr, 2023
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, the number of elements increases by 1. This has applications 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
# Till we have start within list length
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))
OutputThe 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]}
Time complexity: O(n^2)
Auxiliary space: O(n)
Method #2: Using enumerate() + slice() + next() + iter() + count()
The combination of the 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 elements and indices in the 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))
OutputThe 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]}
Time complexity: O(n)
Auxiliary space: O(n)
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 = {}
# calling the recursive function and print the result
print(recursive_slice(test_list, 0, 1, result))
Output{1: [1], 2: [2, 3], 3: [4, 5, 6], 4: [7, 8, 9, 10]}
Time complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using itertools
Python3
# Python3 code to demonstrate working of
# Incremental slice partition in list
# Using itertools
import itertools
# 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 itertools
res = {}
start = 0
# slice partioning the list
for i in itertools.count(1):
slice_len = min(i, len(test_list) - start)
if slice_len == 0:
break
res[i] = test_list[start: start + slice_len]
start += slice_len
# printing result
print("The partitioned dictionary from list is: " + str(res))
OutputThe 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]}
Time complexity: O(n^2), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Similar Reads
Python | Sliced Product in List Accessing elements in a list has many types and variations. These are an essential part of Python programming and one must know to perform the same. This article discusses ways to fetch the initial K elements and do its multiplication. Letâs discuss a certain solution to perform this task. Method #1
5 min read
Slice a 2D List in Python Slicing a 2D list in Python is a common task when working with matrices, tables, or any other structured data. It allows you to extract specific portions of the list, making it easier to manipulate and analyze the data. In this article, we'll explore four simple and commonly used methods to slice a
4 min read
Python - Incremental Sublist Sum 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 of summation of subsequent size i. Letâs discuss certain ways in which this can be done.
3 min read
Split list into lists by value - Python The goal here is to split a list into sublists based on a specific value in Python. For example, given a list [1, 4, 5, 6, 4, 7, 4, 8] and a chosen value 4, we want to split the list into segments that do not contain the value 4, such as [[1], [5, 6], [7], [8]]. Letâs explore different approaches to
4 min read
Python | Increase list size by padding each element by N Given a list and an integer N, write a Python program to increase the size of the list by padding each element by N. Examples: Input : lst = [1, 2, 3] N = 3 Output : [1, 1, 1, 2, 2, 2, 3, 3, 3] Input : lst = ['cats', 'dogs'] N = 2 Output : ['cats', 'cats', 'dogs', 'dogs'] Approach #1 : List comprehe
2 min read
Slice a 2D Array in Python We are given a 2D array matrix and we have to Slice this matrix 2D array and return the result. In this article, we will see how we can Slice the 2D array in Python. Example Input : matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] Output : [[ 4, 5, 6]] Explanation : We can see that 2D array (matrix) is
3 min read
Python - Convert a list into tuple of lists When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists.For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this con
3 min read
Python | Get elements till particular element in list Sometimes, while working with Python list, we can have a requirement in which we need to remove all the elements after a particular element, or, get all elements before a particular element. These both are similar problems and having a solution to it is always helpful. Let's discuss certain ways in
7 min read
Python - Split list into all possible tuple pairs Given a list, the task is to write a python program that can split it into all possible tuple pairs combinations. Input : test_list = [4, 7, 5, 1, 9] Output : [[4, 7, 5, 1, 9], [4, 7, 5, (1, 9)], [4, 7, (5, 1), 9], [4, 7, (5, 9), 1], [4, (7, 5), 1, 9], [4, (7, 5), (1, 9)], [4, (7, 1), 5, 9], [4, (7,
3 min read
Insert list in another list - Python We are given two lists, and our task is to insert the elements of the second list into the first list at a specific position. For example, given the lists a = [1, 2, 3, 4] and b = [5, 6], we want to insert list 'b' into 'a' at a certain position, resulting in the combined list a = [1, 2, 5, 6, 3, 4]
3 min read