Python | Assign range of elements to List
Last Updated :
06 Apr, 2023
Assigning elements to list is a common problem and many varieties of it have been discussed in the previous articles. This particular article discusses the insertion of range of elements in the list. Let's discuss certain ways in which this problem can be solved.
Method #1 : Using extend() This can be solved using the extend function in which the insertion of range of numbers can be done on the rear end using the range function.
Python3
# Python3 code to demonstrate
# Assigning range of elements to List
# using extend()
# initializing list
test_list = [3, 5, 6, 8]
# printing original list
print("The original list : " + str(test_list))
# using extend()
# Assigning range of elements to List
test_list.extend(range(6))
# print result
print("The list after adding range elements : " + str(test_list))
Output : The original list : [3, 5, 6, 8]
The list after adding range elements : [3, 5, 6, 8, 0, 1, 2, 3, 4, 5]
Time Complexity: O(n) where m and n is the number of elements in the list. extend performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2 Using * operator This problem can also be solved using the * operator and with the advantage of flexibility of addition of elements at any position.
Python3
# Python3 code to demonstrate
# Assigning range of elements to List
# using * operator
# initializing list
test_list = [3, 5, 6, 8]
# printing original list
print("The original list : " + str(test_list))
# using * operator
# Assigning range of elements to List
res = [3, 5, *range(6), 6, 8]
# print result
print("The list after adding range elements : " + str(res))
Output : The original list : [3, 5, 6, 8]
The list after adding range elements : [3, 5, 0, 1, 2, 3, 4, 5, 6, 8]
Time complexity: O(1), since we are just adding a range of elements to the list and the length of the range is fixed.
Auxiliary space: O(1), since we are not creating any additional data structures or variables, and the range is generated on the fly without storing it in memory.
Method #3 Using the list() function:
Assign a range of elements to a list in Python using the list() function, you can follow these steps:
- Initialize a list with the elements that you want to include.
- Use the list() function to create a new list from the range of elements that you want to add.
- Use slice notation to insert the new list at the desired position in the original list.
Python3
# Python3 code to demonstrate
# initialize the list
test_list = [3, 5, 6, 8]
# print the original list
print("The original list :", test_list)
# create a new list from the range of elements to be added
new_list = list(range(6))
# use slice notation to insert the new list at the desired position in the original list
test_list = test_list[:2] + new_list + test_list[2:]
# print the resulting list
print("The list after adding range elements:", test_list)
# This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list : [3, 5, 6, 8]
The list after adding range elements: [3, 5, 0, 1, 2, 3, 4, 5, 6, 8]
Time complexity: O(n)
Auxiliary space: O(n).
Method #4 Using += operator:
Python3
# initializing list
test_list = [3, 5, 6, 8]
# printing original list
print("The original list : " + str(test_list))
# using += operator
test_list += list(range(6))
# print result
print("The list after adding range elements : " + str(test_list))
#THis code is contributed by Jyothi pinjala.
OutputThe original list : [3, 5, 6, 8]
The list after adding range elements : [3, 5, 6, 8, 0, 1, 2, 3, 4, 5]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 5: Using the append() method.
This code initializes a list, prints the original list, and then uses a for loop to append the elements in the range 0 to 5 to the list. Finally, it prints the updated list.
Python3
# initializing list
test_list = [3, 5, 6, 8]
# printing original list
print("The original list : " + str(test_list))
# using append() method to add elements
for i in range(6):
test_list.append(i)
# print result
print("The list after adding range elements : " + str(test_list))
OutputThe original list : [3, 5, 6, 8]
The list after adding range elements : [3, 5, 6, 8, 0, 1, 2, 3, 4, 5]
Time complexity: O(n), where n is the number of elements being added to the list.
Auxiliary space: O(n)
Method 6: Using the insert() method.
Step-by-step approach:
- The program initializes a list test_list with four integer values.
- It prints the original list using the print() function and string concatenation.
- It creates a new list by using the range() function to generate a sequence of numbers from 0 to 5 and converting it to a list using the list() function.
- It uses the += operator to concatenate the new list to the original list.
- It prints the updated list using the print() function and string concatenation.
Below is the implementation of the above approach:
Python3
# initializing list
test_list = [3, 5, 6, 8]
# printing original list
print("The original list : " + str(test_list))
# using insert() method to add elements
for i in range(6):
test_list.insert(len(test_list), i)
# print result
print("The list after adding range elements : " + str(test_list))
OutputThe original list : [3, 5, 6, 8]
The list after adding range elements : [3, 5, 6, 8, 0, 1, 2, 3, 4, 5]
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(1) because we are modifying the original list in place and not creating any additional data structures.
Similar Reads
Remove last K elements of list - Python
Given a list and an integer K, the task is to remove the last K elements from the list. For example, if the list is [1, 2, 3, 4, 5] and K = 2, the result should be [1, 2, 3]. Letâs explore different methods to remove the last K elements from a list in Python.Using list slicingList slicing is one of
3 min read
Python - Check if List contains elements in Range
Checking if a list contains elements within a specific range is a common problem. In this article, we will various approaches to test if elements of a list fall within a given range in Python. Let's start with a simple method to Test whether a list contains elements in a range.Using any() Function -
3 min read
Print List Elements in Circular Range - Python
We are given a list and two indices, start and end. The task is to print the elements of the list starting from the start index to the end index, considering the list circularly. For example, if the list is [1, 2, 3, 4, 5], the start index is 3, and the end index is 1, the output should be [4, 5, 1,
3 min read
Python - Move Element to End of the List
We are given a list we need to move particular element to end to the list. For example, we are given a list a=[1,2,3,4,5] we need to move element 3 at the end of the list so that output list becomes [1, 2, 4, 5, 3].Using remove() and append()We can remove the element from its current position and th
3 min read
Python - List of float to string conversion
When working with lists of floats in Python, we may often need to convert the elements of the list from float to string format. For example, if we have a list of floating-point numbers like [1.23, 4.56, 7.89], converting them to strings allows us to perform string-specific operations or output them
3 min read
Python - Delete elements in range
In Python, we often need to remove elements from a list within a specific range. This can be useful when cleaning data or modifying lists based on certain conditions. Using List Comprehension List comprehension is one of the most Pythonic and efficient ways to filter out elements. It is concise, eas
3 min read
Python Program to Swap Two Elements in a List
In this article, we will explore various methods to swap two elements in a list in Python. The simplest way to do is by using multiple assignment.Example:Pythona = [10, 20, 30, 40, 50] # Swapping elements at index 0 and 4 # using multiple assignment a[0], a[4] = a[4], a[0] print(a)Output[50, 20, 30,
1 min read
Python - Filter Rows with Range Elements
Given a Matrix, filter all the rows which contain all elements in the given number range. Input : test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]], i, j = 2, 5 Output : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]] Explanation : 2, 3, 4, 5 all are present in above rows. Input :
5 min read
Python program to insert an element into sorted list
Inserting an element into a sorted list while maintaining the order is a common task in Python. It can be efficiently performed using built-in methods or custom logic. In this article, we will explore different approaches to achieve this.Using bisect.insort bisect module provides the insort function
2 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