Python – Substitute prefix part of List

PythonServer Side ProgrammingProgramming

When it is required to substitute prefix part of a list, the ‘len’ method and the ‘:’ operator is used.

Example

Below is a demonstration of the same −

my_list_1 = [15, 44, 82]
my_list_2 = [29, 77, 19, 44, 26, 18]

print("The first list is : " )
print(my_list_1)

print("The second list is : " )
print(my_list_2)

print("The first list after sorting is :")
my_list_1.sort()
print(my_list_1)

print("The first list after sorting is :")
my_list_2.sort()
print(my_list_2)

my_result = my_list_1 + my_list_2[len(my_list_1) : ]

print("The resultant list is : ")
print(my_result)

Output

The first list is :
[15, 44, 82]
The second list is :
[29, 77, 19, 44, 26, 18]
The first list after sorting is :
[15, 44, 82]
The first list after sorting is :
[18, 19, 26, 29, 44, 77]
The resultant list is :
[15, 44, 82, 29, 44, 77]

Explanation

  • Two lists are defined and are displayed on the console.

  • They are sorted and displayed on the console.

  • Both the lists are added, wherein in the second list, a specific length of the list is taken.

  • This result is assigned to a variable.

  • This is displayed as the output on the console.

raja
Updated on 13-Sep-2021 07:39:39

Advertisements