Python | Equidistant element list
Last Updated :
06 Apr, 2023
Sometimes, while working with Python list we can have a problem in which we need to construct the list in which the range is auto computed using the start, end and length parameters. The solution of this problem can have many applications. Let's discuss a way in which this task can be performed.
Method : Using list comprehension This task can be performed using list comprehension, shorthand for the loop version of logic. In this, we just compute the range using division manipulation and extend it to increasing list forming equidistant list.
Python3
# Python3 code to demonstrate working of
# Equidistant element list
# using list comprehension
# initializing start value
strt = 5
# initializing end value
end = 10
# initializing length
length = 8
# Equidistant element list
# using list comprehension
test_list = [strt + x * (end - strt)/length for x in range(length)]
# Printing result
print("The Equidistant list is : " + str(test_list))
Output :
The Equidistant list is : [5.0, 5.625, 6.25, 6.875, 7.5, 8.125, 8.75, 9.375]
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 list test_list
Using itertools:
This code uses the itertools.count() function to generate a sequence of numbers starting from the value "strt" and incrementing by the value (end - strt)/length. 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 an equidistant list of numbers between the start and end values.
Python3
import itertools
# initializing start value
strt = 5
# initializing end value
end = 10
# initializing length
length = 8
# Equidistant element list using itertools
test_list = list(itertools.islice(itertools.count(strt, (end - strt)/length), length))
# Printing result
print("The Equidistant list is : " + str(test_list))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe Equidistant list is : [5, 5.625, 6.25, 6.875, 7.5, 8.125, 8.75, 9.375]
Time complexity: O(n)
Auxiliary space: O(n) where n is the number of elements in the list.
Similar Reads
Python | Find missing elements in List Sometimes, we can get elements in range as input but some values are missing in otherwise consecutive range. We might have a use case in which we need to get all the missing elements. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension We can perform the task o
7 min read
Python - Adjacent elements in List Given a List extract both next and previous element for each element. Input : test_list = [3, 7, 9, 3] Output : [(None, 7), (3, 9), (7, 3), (9, None)] Explanation : for 7 left element is 3 and right, 9. Input : test_list = [3, 7, 3] Output : [(None, 7), (3, 3), (7, None)] Explanation : for 7 left el
3 min read
Uncommon Elements in Lists of List - Python We are given two lists of lists and our task is to find the sublists that are uncommon between them. (a sublist is considered uncommon if it appears in only one of the lists and not in both.) For example: a = [[1, 2], [3, 4], [5, 6]] and b = [[3, 4], [5, 7], [1, 2]] then the output will be [[5, 6],
4 min read
Python - Optional padding in list elements Optional padding in list elements involves adding extra elements, such as zeros or placeholders, to a list to ensure it reaches a desired length. This technique is useful when working with fixed-size data structures or when aligning data in lists.Using List ComprehensionThis method pads each element
2 min read
Python - Check if all elements in List are same To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. Pythona = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) OutputTrue Explanation:Converting
3 min read