Python | Variable list slicing
The problem of slicing a list has been dealt earlier, but sometimes we need to perform the slicing in variable lengths according to the input given in other list. This problem has its potential application in web development. Let’s discuss certain ways in which this can be done.
Method #1 : Using itertools.islice()
+ list comprehension
The list comprehension can be used to iterate through the list and the component issue is solved using the islice function.
# Python3 code to demonstrate # variable length slicing # using itertools.islice() + list comprehension from itertools import islice # initializing test list test_list = [ 1 , 5 , 3 , 7 , 8 , 10 , 11 , 16 , 9 , 12 ] # initializing slice list slice_list = [ 2 , 1 , 3 , 4 ] # printing original list print ( "The original list : " + str (test_list)) # printing slice list print ( "The slice list : " + str (slice_list)) # using itertools.islice() + list comprehension # variable length slicing temp = iter (test_list) res = [ list (islice(temp, part)) for part in slice_list] # print result print ( "The variable sliced list is : " + str (res)) |
The original list : [1, 5, 3, 7, 8, 10, 11, 16, 9, 12] The slice list : [2, 1, 3, 4] The variable sliced list is : [[1, 5], [3], [7, 8, 10], [11, 16, 9, 12]]
Method #2 : Using zip() + accumulate()
+ list slicing
Apart from using the list comprehension to perform the task of binding, this method uses zip function to hold sublist element together, accumulate function joins the elements, and slicing is used to construct the required slicing.
# Python3 code to demonstrate # variable length slicing # using zip() + accumulate() + list slicing from itertools import accumulate # initializing test list test_list = [ 1 , 5 , 3 , 7 , 8 , 10 , 11 , 16 , 9 , 12 ] # initializing slice list slice_list = [ 2 , 1 , 3 , 4 ] # printing original list print ( "The original list : " + str (test_list)) # printing slice list print ( "The slice list : " + str (slice_list)) # using zip() + accumulate() + list slicing # variable length slicing res = [test_list[i - j: i] for i, j in zip (accumulate(slice_list), slice_list)] # print result print ( "The variable sliced list is : " + str (res)) |
The original list : [1, 5, 3, 7, 8, 10, 11, 16, 9, 12] The slice list : [2, 1, 3, 4] The variable sliced list is : [[1, 5], [3], [7, 8, 10], [11, 16, 9, 12]]
Recommended Posts:
- Python List Comprehension and Slicing
- Python | Custom slicing in List
- Python | Alternate range slicing in list
- Python | Get the substring from given string using list slicing
- Program to cyclically rotate an array by one in Python | List Slicing
- Python | Convert 1D list to 2D list of variable length
- 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)
- Basic Slicing and Advanced Indexing in NumPy Python
- Python Slicing | Reverse an array in groups of given size
- String slicing in Python to check if a string can become empty by recursive deletion
- String slicing in Python to rotate a string
- __name__ (A Special variable) in Python
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.