Open In App

Python | Interval Initialization in list

Last Updated : 26 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Interval Initialization in list means creating a list in Python where specific intervals are initialized with certain values. 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. 

Using Numpy

NumPy is a dedicated library for numerical computing that makes interval initialization in lists efficient, using array slicing and boolean masks for faster data selection.

Example:

Python
import numpy as np
a = list(range(50))

# interval elements
N = 5

# interval difference
K = 15

# Using numpy for efficient interval initializing
b = np.array(a)
mask = np.tile([True] * N + [False] * (K - N), len(a) // K + 1)[:len(a)]
res = b[mask]
print(res)

Output
[ 0  1  2  3  4 15 16 17 18 19 30 31 32 33 34 45 46 47 48 49]

Explanation:

  • Initialize List and Parameters: a = list(range(50)) creates a list from 0 to 49. N = 5 specifies the number of consecutive elements in each interval, and K = 15 specifies the total length of each interval.
  • Convert List to NumPy Array: b = np.array(a) converts the list a to a NumPy array.
  • Create Interval Mask: mask = np.tile([True] * N + [False] * (K – N), len(a) // K + 1)[:len(a)] creates a boolean mask where True values indicate the positions of elements to include in the intervals. The pattern [True] * N + [False] * (K – N) is repeated enough times to cover the length of a and then sliced to fit exactly.
  • Apply Mask to Array: res = b[mask] uses the mask to select elements from the array b where the mask is True.

Using itertools.compress() and itertools.cycle()

It provide functional approach to interval initialization in lists. They allow efficient selection of elements at regular intervals by cycling through a pattern of True and False values.

Example:

Python
from itertools import compress, cycle
a = list(range(50))

# interval elements
N = 5

# interval difference
K = 15

# using itertools.compress() + itertools.cycle()
b = cycle([True] * N + [False] * (K - N))
res = list(compress(a, b))
print(res)

Output
[0, 1, 2, 3, 4, 15, 16, 17, 18, 19, 30, 31, 32, 33, 34, 45, 46, 47, 48, 49]

Explanation:

  • Create Interval Pattern: b = cycle([True] * N + [False] * (K – N)) creates an infinite cycle of a pattern where True values indicate the positions of elements to include in the intervals. The pattern [True] * N + [False] * (K – N) is repeated indefinitely.
  • Apply Pattern to List: res = list(compress(a, b)) uses itertools.compress to filter the list a according to the pattern b. This selects elements from a where the corresponding value in the pattern b is True.

Using List Comprehension

List comprehension is a simple way to select elements at regular intervals, but it can be inefficient for large datasets due to Python’s loop-based processing.

Example:

Python
a = list(range(50))

# interval elements
N = 5

# interval difference
K = 15

# List comprehension method for interval initializing
res = [a[i] for i in range(len(a)) if i % K < N]
print(res)

Output
[0, 1, 2, 3, 4, 15, 16, 17, 18, 19, 30, 31, 32, 33, 34, 45, 46, 47, 48, 49]

Explanation:

  • res = [a[i] for i in range(len(a)) if i % K < N] constructs a new list by including elements from a where the index i satisfies the condition i % K < N. This condition ensures that for every block of K elements, only the first N elements are included.


Next Article

Similar Reads