Python get range in list

Python Range to List

Python Range object is an iterable that creates elements lazily. In other words all the elements of the range are not stored in memory. Only that element during an iteration is returned by the range object. Python List is not like that. All the elements of a list are stored in memory.

Python Range can save memory resources while list can offer speed. So, based on the requirement, you may need to convert a Python range object to a list.

To convert a Python Range to Python List, use list() constructor, with range object passed as argument. list() constructor returns a list generated using the range. Or you can use a for loop to append each item of range to list.

In this tutorial, we will learn how to convert a Python range object to a list, with the help of list() constructor and for loop.

Example 1 – Python Range to List using list()

In this example, we will create a range object and then convert this range object to list.

Python Program

range_1 = range(2, 20, 3) list_1 = list(range_1) print(list_1)

Example 2 – Python Range to List using For Loop

You can also use Python For Loop, to iterate over each element of range, and then append the item to list.

In this example, we will create a range object and then convert this range object to list.

Читайте также:  Css следы от пуль

Python Program

range_1 = range(2, 20, 3) list_1 = list() for x in range_1 : list_1.append(x) print(list_1)

Conclusion

In this Python Tutorial, we learned how to convert a Python range to Python List using list() constructor or for loop.

Источник

Python – Random range in list

Input : test_list = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
Output : [5, 7, 2, 1]
Explanation : A random range elements are extracted of any length.

Input : test_list = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
Output : [4, 8]
Explanation : A random range elements are extracted of any length.

Method : Using randrange() + slicing

randrange(): Python offers a function that can generate random numbers from a specified range and also allowing rooms for steps to be included, called randrange() in random module.

In this, we extract index of list, and for first index, and then again employ the function to get end part of range using randrange() from start index to list length. The range is extracted using slicing.

Python3

The original list is : [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4] Required List : [19] The original list is : [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4] Required List : [10, 13, 5, 7] The original list is : [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4] Required List : []

Time Complexity: O(n) where n is the number of elements in the list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list

Method: Using random.sample()

Import the random module.
Define a function randomRangeList(test_list) that takes an input list as argument.
Get the length of the input list using the len() function and store it in a variable list_len.
Generate two distinct random indices using the random.sample() function. The first argument to random.sample() should be a range object created using range(), with the start value being 0 and the stop value being list_len – 1. The second argument should be the number of random indices to generate, which is 2 in this case. Store the two indices in variables start_idx and end_idx.
Sort the two indices in ascending order using the sorted() function and store them back in start_idx and end_idx. This is necessary because the random.sample() function does not guarantee that the indices will be generated in sorted order.
Slice the input list using the indices start_idx and end_idx, and return the resulting sublist.
Test the function with some input lists.

Python3

The original list is : [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4] Required List : [7, 2] The original list is : [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4] Required List : [4, 8, 10, 13, 5, 7, 2] The original list is : [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4] Required List : [8, 10, 13, 5, 7]

Time complexity: O(1).
Auxiliary space: O(1) auxiliary space to store the two random indices and the length of the input list.

METHOD 3:Using loop

The program takes an original list as input and creates four different required lists based on the given criteria. The first required list contains elements 7 and 2 from the original list, the second required list contains elements between 4 and 14 from the original list, the third required list contains elements from index 3 to index 7 from the original list, and the fourth required list contains elements from index 3 to index 7 from the original list that are greater than 5.

1.Create an original list of elements.
2.Create an empty list to hold the required elements.
3.For Required List 1, iterate over each element in the original list and check if it is equal to 7 or 2. If it is, append it to the required list.
4.For Required List 2, iterate over each element in the original list and check if it is greater than 3 and less than 14. If it is, append it to the required list.
5.For Required List 3, iterate over each index in the range of the length of the original list and check if it is between 3 and 7 (inclusive). If it is, append the element at that index to the required list.
6.For Required List 4, iterate over each index in the range of the length of the original list and check if it is between 3 and 7 (inclusive) and if the element at that index is greater than 5. If it is, append the element to the required list.
7.Print the required lists.

Источник

Оцените статью