Python | Custom slicing in List Last Updated : 13 Apr, 2023 Comments Improve Suggest changes Like Article Like Report Sometimes, while working with Python, we can come to a problem in which we need to perform the list slicing. There can be many variants of list slicing. One can have custom slice interval and slicing elements. Let's discuss problem to such problem. Method : Using compress() + cycle() The combination of above functions can be used to perform this particular task. In this, we filter the list for Truth value of required elements and eliminate those which should be skipped by providing then a boolean false. Then the result is accumulated using inbuilt compress() Python3 # Python3 code to demonstrate working of # Custom slicing in List # using compress() + cycle() from itertools import cycle, compress # initialize lists test_list = [1, 2, 4, 7, 3, 8, 6, 2, 10, 11, 17, 34, 23, 21] # printing original list print("The original list is : " + str(test_list)) # initialize interval interval = 5 # initialize element number ele_num = 4 # Custom slicing in List # using compress() + cycle() temp = cycle([True] * ele_num + [False] * interval) res = list(compress(test_list, temp)) # printing result print("Custom sliced list is : " + str(res)) Output : The original list is : [1, 2, 4, 7, 3, 8, 6, 2, 10, 11, 17, 34, 23, 21] Custom sliced list is : [1, 2, 4, 7, 11, 17, 34, 23] Time Complexity: O(n), where n is the length of the list test_list Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list Using filter() function: Approach: We can use the filter() function to perform custom slicing in a list. The filter() function returns an iterator containing the elements from the original list that satisfy a given condition. We can pass a lambda function to the filter() function to specify the condition. Python3 lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] new_lst = list(filter(lambda x: x % 2 == 0, lst)) print(new_lst) Output[2, 4, 6, 8, 10] Time complexity: O(n)Space complexity: O(n) Comment More infoAdvertise with us Next Article Python | Custom slicing in List manjeet_04 Follow Improve Article Tags : Python Python list-programs Practice Tags : python Similar Reads Python List Slicing Python list slicing is fundamental concept that let us easily access specific elements in a list. In this article, weâll learn the syntax and how to use both positive and negative indexing for slicing with examples.Example: Get the items from a list starting at position 1 and ending at position 4 (e 4 min read Python List Comprehension with Slicing Python's list comprehension and slicing are powerful tools for handling and manipulating lists. When combined, they offer a concise and efficient way to create sublists and filter elements. This article explores how to use list comprehension with slicing including practical examples. The syntax for 3 min read Reversing a List in Python In this article, we are going to explore multiple ways to reverse a list. Python provides several methods to reverse a list using built-in functions and manual approaches. The simplest way to reverse a list is by using the reverse() method. Let's take an example to reverse a list using reverse() met 4 min read JS Equivalent to Python Slicing In Python, slicing is a feature that allows us to extract portions of a sequence such as lists, strings and tuples using a simple syntax. However, JavaScript does not have a direct equivalent to Pythonâs slicing syntax. Fortunately, there are several ways to achieve similar functionality in JavaScri 4 min read range() to a list in Python In Python, the range() function is used to generate a sequence of numbers. However, it produces a range object, which is an iterable but not a list. If we need to manipulate or access the numbers as a list, we must explicitly convert the range object into a list. For example, given range(1, 5), we m 2 min read Python slicing multi-dimensional arrays Python's NumPy package makes slicing multi-dimensional arrays a valuable tool for data manipulation and analysis. It enables efficient subset data extraction and manipulation from arrays, making it a useful skill for any programmer, engineer, or data scientist.Python Slicing Multi-Dimensional Arrays 4 min read Python slice() function In this article, we will learn about the Python slice() function with the help of multiple examples. Example Python3 String = 'Hello World' slice_obj = slice(5,11) print(String[slice_obj]) Output: World A sequence of objects of any type (string, bytes, tuple, list, or range) or the object which im 5 min read String Slicing in Python String slicing in Python is a way to get specific parts of a string by using start, end and step values. Itâs especially useful for text manipulation and data parsing.Letâs take a quick example of string slicing:Pythons = "Hello, Python!" print(s[0:5])OutputHello Explanation: In this example, we use 4 min read Convert Tuple to List in Python In Python, tuples and lists are commonly used data structures, but they have different properties:Tuples are immutable: their elements cannot be changed after creation.Lists are mutable: they support adding, removing, or changing elements.Sometimes, you may need to convert a tuple to a list for furt 2 min read Map vs List comprehension - Python List comprehension and map() both transform iterables but differ in syntax and performance. List comprehension is concise as the logic is applied in one line while map() applies a function to each item and returns an iterator and offering better memory efficiency for large datasets.List comprehensio 2 min read Like