- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python β Substitute prefix part of List
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.
Advertisements