Python | Decimal step range in list
Last Updated :
13 Apr, 2023
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
# initializing start value
strt = 5
# initializing step value
step = 0.4
# using list comprehension
# Decimal step range
test_list = [strt + (x * step)
for x in range(0, 5)]
# Printing result
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
# restep set to True
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
# Printing all numbers from 1 to
# 2 in steps of 0.1
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
# initializing start value
start = 5.0
# initializing step value
step = 0.4
# initializing length
length = 5
# Using itertools to create a list of decimal numbers with a specific step
test_list = list(itertools.islice(itertools.count(start, step), length))
# Printing result
print("The list after decimal range value initialization : " + str(test_list))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe 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.
Similar Reads
Python - Specific Range Addition in List Sometimes, while working with Python, we need to perform an edition in the Python list. And sometimes we need to perform this to a specific index range in it. This kind of application can have applications in many domains. Let us discuss certain ways in which this task can be performed. Method #1:
4 min read
Python | Alternate range slicing in list List slicing is quite common utility in Python, one can easily slice certain elements from a list, but sometimes, we need to perform that task in non-contiguous manner and slice alternate ranges. Let's discuss how this particular problem can be solved. Method #1 : Using list comprehension List compr
6 min read
Python program to reverse a range in list Reversing a specific range within a list is a common task in Python programming that can be quite useful, when data needs to be rearranged. In this article, we will explore various approaches to reverse a given range within a list. We'll cover both simple and optimized methods. One of the simplest w
3 min read
Python - Element Index in Range Tuples Sometimes, while working with Python data, we can have a problem in which we need to find the element position in continuous equi ranged tuples in list. This problem has applications in many domains including day-day programming and competitive programming. Let's discuss certain ways in which this t
7 min read
Standard deviation of list - Python We are given a list of numbers, and our task is to compute the standard deviation. Standard deviation is a measure of how spread out the numbers are from the mean. A low standard deviation means the values are close to the mean, while a high standard deviation indicates the values are spread out. Fo
2 min read