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
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]
- Python | Variable list slicing
- Python List Comprehension and Slicing
- Python | Get the substring from given string using list slicing
- Python | Alternate range slicing in list
- Program to cyclically rotate an array by one in Python | List Slicing
- Python | Custom list split
- Python | Custom Cycle list
- Python | Custom Multiplication in list of lists
- Python | Slicing list from Kth element to last element
- Python | Reverse Slicing of given string
- Python Slicing | Extract ‘k’ bits from a given position
- Interesting facts about strings in Python | Set 2 (Slicing)
- Python Slicing | Reverse an array in groups of given size
- Basic Slicing and Advanced Indexing in NumPy Python
- Python | Custom length Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.