Open In App
Related Articles

Python | Interval Initialization in list

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

# 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]

The time complexity of this approach is O(N), where N is the length of the input list.

The auxiliary space required is also O(N), where N is the length of the input list. 

  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

# 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]

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 

Method #3 : Using slicing and looping

The function takes a list test_list, an integer N, and an integer K as input. It initializes an empty list res and an index i to zero. Then, it enters a while loop that will continue until i becomes greater than or equal to the length of test_list. Inside the loop, it adds the elements from test_list to res, starting at index i and ending at index i + N. It then increases i by `K

Python3

def interval_init(test_list, N, K):
    # Initialize an empty result list
    res = []
    # Set the index to zero
    i = 0
    # While the index is less than the length of the original list
    while i < len(test_list):
        # Add the elements from the original list to the result list
        # starting at the current index and ending at the current index + N
        res += test_list[i:i+N]
        # Increase the index by K
        i += K
    # Return the result list
    return res
 
# Initialize a list with the numbers from 0 to 49
test_list = list(range(50))
# Call the function and print the result
print(interval_init(test_list, 5, 15))
#This code is contributed by Edula Vinay Kumar Reddy

                    

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

This approach has a time complexity of O(n), where n is the length of the original list. The space complexity is also O(n), as we are creating a new list to store the result.

This approach is similar to the first method using list comprehension, but it avoids the use of enumerate and modulo operator. It simply uses a while loop to iterate through the original list and add the desired elements to the result list.



Last Updated : 20 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads