Python | Assign range of elements to List
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)) |
The original list : [3, 5, 6, 8] The list after adding range elements : [3, 5, 6, 8, 0, 1, 2, 3, 4, 5]
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)) |
The original list : [3, 5, 6, 8] The list after adding range elements : [3, 5, 0, 1, 2, 3, 4, 5, 6, 8]
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 |
The original list : [3, 5, 6, 8] The list after adding range elements: [3, 5, 0, 1, 2, 3, 4, 5, 6, 8]
This approach has a time complexity of O(n) and a space complexity of 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. |
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)
Auxiliary Space: O(N)
Please Login to comment...