Open In App

# Python – Substitute prefix part of List

Given 2 list, substitute one list as prefix elements of other.

Input: test_list1 = [4, 6, 8, 7], test_list2 = [2, 7, 9, 4, 2, 8]
Output : [4, 6, 8, 7, 2, 8]
Explanation: 4, 6, 8, 7 from list 1 and rest, 2 and 8 from list 2, substituting prefix of list 2.

Input: test_list1 = [4, 6], test_list2 = [2, 7, 9, 4, 2, 8]
Output : [4, 6, 9, 4, 2, 8]
Explanation: 4, 6 from list 1 and rest, 9, 4, 2 and 8 from list 2, substituting prefix of list 2.

Method #1 : Using len() + list slicing

In this, we add the list 1 and then part of list 2 after size of list 1, using len() and list slicing.

## Python3

 `# Python3 code to demonstrate working of``# Substitute prefix part of List``# Using len() + list slicing` `# initializing lists``test_list1 ``=` `[``4``, ``6``, ``8``, ``7``]``test_list2 ``=` `[``2``, ``7``, ``9``, ``4``, ``2``, ``8``, ``6``, ``4``, ``1``, ``10``]` `# printing original lists``print``(``"The original list 1 : "` `+` `str``(test_list1))``print``(``"The original list 2 : "` `+` `str``(test_list2))` `# size slicing after length of list 1``res ``=` `test_list1 ``+` `test_list2[``len``(test_list1) : ]` `# printing result``print``(``"The joined list : "` `+` `str``(res))`

Output

```The original list 1 : [4, 6, 8, 7]
The original list 2 : [2, 7, 9, 4, 2, 8, 6, 4, 1, 10]
The joined list : [4, 6, 8, 7, 2, 8, 6, 4, 1, 10]```

Time Complexity: O(n), where n is the length of the second list. The program performs a single operation which is slicing the second list and concatenating it with the first list. Slicing a list of size n takes O(n) time. The concatenation operation is O(1) as it is creating a new reference to the list rather than creating a new list. Therefore the time complexity is O(n) .
Auxiliary Space: O(n), as it is creating a new list that contains the elements of the first list and the sliced elements of the second list.

Method #2 : Using * operator

In this, we use * operator to perform task of packing and unpacking it to new list.

## Python3

 `# Python3 code to demonstrate working of``# Substitute prefix part of List``# Using * operator` `# initializing lists``test_list1 ``=` `[``4``, ``6``, ``8``, ``7``]``test_list2 ``=` `[``2``, ``7``, ``9``, ``4``, ``2``, ``8``, ``6``, ``4``, ``1``, ``10``]` `# printing original lists``print``(``"The original list 1 : "` `+` `str``(test_list1))``print``(``"The original list 2 : "` `+` `str``(test_list2))` `# * operator reconstructs lists``res ``=` `[``*``test_list1, ``*``test_list2[``len``(test_list1) : ]]` `# printing result``print``(``"The joined list : "` `+` `str``(res))`

Output

```The original list 1 : [4, 6, 8, 7]
The original list 2 : [2, 7, 9, 4, 2, 8, 6, 4, 1, 10]
The joined list : [4, 6, 8, 7, 2, 8, 6, 4, 1, 10]```

Time complexity: O(n), where n is the length of the longer list (either “test_list1” or “test_list2”). The “*” operator is used to concatenate the lists, which takes linear time proportional to the length of the lists being concatenated.
Auxiliary space: O(n), where n is the length of the combined list “res”.

Method #3 : Using extend()+len()+list slicing

## Python3

 `# Python3 code to demonstrate working of``# Substitute prefix part of List``# Using len() + extend()+ list slicing` `# initializing lists``test_list1 ``=` `[``4``, ``6``, ``8``, ``7``]``test_list2 ``=` `[``2``, ``7``, ``9``, ``4``, ``2``, ``8``, ``6``, ``4``, ``1``, ``10``]` `# printing original lists``print``(``"The original list 1 : "` `+` `str``(test_list1))``print``(``"The original list 2 : "` `+` `str``(test_list2))` `# size slicing after length of list 1``x``=``len``(test_list1)``test_list1.extend(test_list2[x:])` `# printing result``print``(``"The joined list : "` `+` `str``(test_list1))`

Output

```The original list 1 : [4, 6, 8, 7]
The original list 2 : [2, 7, 9, 4, 2, 8, 6, 4, 1, 10]
The joined list : [4, 6, 8, 7, 2, 8, 6, 4, 1, 10]```

Time Complexity: O(N)
Auxiliary Space: O(1)

Method 4: Using itertools.chain() method

• Import the itertools module.
• Use the chain() method to concatenate the prefix list and the remaining elements of the second list.
• Convert the result into a list.
• Assign the result to a new variable.

## Python3

 `# Python3 code to demonstrate working of``# Substitute prefix part of List``# Using itertools.chain() method` `# importing itertools module``import` `itertools` `# initializing lists``test_list1 ``=` `[``4``, ``6``, ``8``, ``7``]``test_list2 ``=` `[``2``, ``7``, ``9``, ``4``, ``2``, ``8``, ``6``, ``4``, ``1``, ``10``]` `# printing original lists``print``(``"The original list 1 : "` `+` `str``(test_list1))``print``(``"The original list 2 : "` `+` `str``(test_list2))` `# using itertools.chain() method to substitute prefix``res ``=` `list``(itertools.chain(test_list1, test_list2[``len``(test_list1):]))` `# printing result``print``(``"The joined list : "` `+` `str``(res))`

Output

```The original list 1 : [4, 6, 8, 7]
The original list 2 : [2, 7, 9, 4, 2, 8, 6, 4, 1, 10]
The joined list : [4, 6, 8, 7, 2, 8, 6, 4, 1, 10]```

Time complexity: O(n), where n is the length of the second list.
Auxiliary space: O(n), since we’re creating a new list to store the result.