Python | Interval Initialization in list
There are numerous ways to initialize the list with the elements, but sometimes, its required to initialize the lists with the numbers in a sliced way. This can be custom and hence knowledge of this can come handy. Let’s discuss certain ways in which this can be done.
Method #1 : Using list comprehension + enumerate()
The list comprehension can do the possible iteration part and enumerate can help in the part of logic and checking for the valid elements required in the list.
# Python3 code to demonstrate # interval initializing in list # using list comprehension + enumerate() # initializing lists test_list = list(range(50)) # printing original list print ("The original list is : " + str(test_list)) # interval elements N = 5 # interval difference K = 15 # using list comprehension + enumerate() # interval initializing in list res = [i for j, i in enumerate(test_list) if j % K < N ] # printing result print ("The modified initialized list : " + str(res)) |
Output :
The original list is : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
The modified initialized list : [0, 1, 2, 3, 4, 15, 16, 17, 18, 19, 30, 31, 32, 33, 34, 45, 46, 47, 48, 49]
Method #2 : Using itertools.compress() + itertools.cycle()
The above two function can combine to facilitate the solution of the discussed problem. The cycle function can to the task of repetition and the compress function can be beneficial when it comes to clubbing the segments together.
# Python3 code to demonstrate # interval initializing in list # using itertools.compress() + itertools.cycle() from itertools import compress, cycle # initializing lists test_list = list(range(50)) # printing original list print ("The original list is : " + str(test_list)) # interval elements N = 5 # interval difference K = 15 # using itertools.compress() + itertools.cycle() # interval initializing in list func = cycle([True] * N + [False] * (K - N)) res = list(compress(test_list, func)) # printing result print ("The modified initialized list : " + str(res)) |
Output :
The original list is : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
The modified initialized list : [0, 1, 2, 3, 4, 15, 16, 17, 18, 19, 30, 31, 32, 33, 34, 45, 46, 47, 48, 49]
Recommended Posts:
- Python | Boolean list initialization
- Python | List Initialization with alternate 0s and 1s
- Counters in Python | Set 1 (Initialization and Updation)
- Python | sympy.Interval().contains() method
- Python | sympy.Interval().inf attribute
- Python | sympy.Interval().intersect() method
- Python Program to Print Numbers in an Interval
- Python program to print all Prime numbers in an Interval
- Python | Convert list of tuples to list of list
- Python | Convert list of string to list of list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Merge list of tuple into list by joining the strings
- Python | Convert list of string into sorted list of integer
- Python | Replace elements in second list with index of same element in first list
- Python | Merge List with common elements in a List of Lists
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.



