Sometimes, while working with a Python list we can have a problem in which we require to populate the list in a range of decimals. Integral ranges are easier to handle using inbuilt constructs, but having a decimal value to provide to range value doesn’t work. Let’s discuss a way in which this problem can be solved.
Method 1: Using list comprehension
One way to solve this problem is by using list comprehension which can solve this problem in an easier way by iterating the list and multiplying the range element to the index of the element.
Python3
strt = 5
step = 0.4
test_list = [strt + (x * step)
for x in range ( 0 , 5 )]
print ("The list after decimal range \
value initialization : " + str (test_list))
|
Output:
The list after decimal range value initialization : [5.0, 5.4, 5.8, 6.2, 6.6]
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 test_list list
Method 2: Using Numpy.linspace
The numpy.linspace() function returns number spaces evenly w.r.t interval. Similar to numpy.arange() function but instead of step it uses sample number.
Python3
import numpy as geek
print ( "Decimal range value" ,
geek.linspace( 2.0 , 3.0 , num = 5 ))
|
Output:
Decimal range value [2. 2.25 2.5 2.75 3. ]
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(1) additional constant space is required
Method 3: Using Numpy.arrange
The np.arange([start,] stop[, step,][, dtype]), Returns an array with evenly spaced elements as per the interval. The interval mentioned is half-opened i.e. [Start, Stop)
Python3
import numpy as np
print (np.arange( 1 , 2 , 0.1 ))
|
Output:
[1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]
Method 4: Using itertools:
The python library itertools provides a function called “count” which can be used to create a sequence of numbers starting from the value “start” and incrementing by the value “step”. Then it uses the itertools.islice() function to take the first “length” number of elements from that sequence and convert it into a list. The resulting list will be a list of decimal numbers with a specific step. The time and space complexity of this approach would be O(n) for both, where n is the number of elements in the list.
Python3
import itertools
start = 5.0
step = 0.4
length = 5
test_list = list (itertools.islice(itertools.count(start, step), length))
print ( "The list after decimal range value initialization : " + str (test_list))
|
Output
The list after decimal range value initialization : [5.0, 5.4, 5.800000000000001, 6.200000000000001, 6.600000000000001]
Time complexity: O(n) where n is the length of the final list. The itertools.count() function generates an infinite iterator of numbers starting from the given start value and incrementing by the given step value. The itertools.islice() function is used to slice this iterator and limit the number of items returned to the specified length. These operations are performed in a single pass over the iterator, resulting in a time complexity of O(n) where n is the length of the final list.
Auxiliary Space: O(n) where n is the length of the final list. A new list is created and populated with the n elements generated by the iterator. The space complexity is O(n) as a list of n elements is created.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!