# Python | Retain K Front and Rear elements

Sometimes, we require to shrink a list by deletion of its certain elements. One of the methods that is employed to perform this particular task is front and rear element retention and deletion of rest elements. It is a good utility whose solution can be useful to have. Letâ€™s discuss certain ways in which this can be performed.

Method #1: Using list slicing + del operator The del operator can be clubbed with the slicing action to delete all but the front and rear elements from a list to obtain a cropped version of list.

## Python3

 `# Python3 code to demonstrate``# Retain K Front and Rear elements``# using del operator + list slicing` `# initializing list``test_list ``=` `[``2``, ``3``, ``5``, ``7``, ``9``, ``10``, ``8``, ``6``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `2` `# using del operator + list slicing``# Retain K Front and Rear elements``N ``=` `len``(test_list)``del` `test_list[K: N ``-` `K]` `# printing result``print``(``"The cropped list is : "` `+` `str``(test_list))`

Output :
```The original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is : [2, 3, 8, 6]```

Time complexity : O(N)
Space complexity : O(1)

Method #2: Using list slicing

Above method can be modified and the use of del operator can be omitted to achieve this particular task. We can slice a list in a way that specific number of elements are removed from the list.

Steps:

1. Initialize the list test_list with values [2, 3, 5, 7, 9, 10, 8, 6].
2. Print the original list using print(“The original list is : ” + str(test_list)).
3. Initialize the variable K with value 2.
4. Use list slicing to create a new list by selecting the first K elements of test_list using test_list[:K], and concatenating it with the last K elements of test_list using test_list[-K:].
5. Store the new list in the variable res.
6. Print the cropped list using print(“The cropped list is : ” + str(res)).

## Python3

 `# Python3 code to demonstrate``# Retain K Front and Rear elements``# using list slicing` `# initializing list``test_list ``=` `[``2``, ``3``, ``5``, ``7``, ``9``, ``10``, ``8``, ``6``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `2` `# using list slicing``# Retain K Front and Rear elements``res ``=` `test_list[: K] ``+` `test_list[``-``K:]` `# printing result``print``(``"The cropped list is : "` `+` `str``(res))`

Output :
```The original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is : [2, 3, 8, 6]```

Time complexity : O(N)
Space complexity : O(N)

Method#3: Using List Comprehension.

Algorithm:

1. Initialize the original list.
2. Print the original list.
3. Initialize the value of K.
4. Using list comprehension, slice the list and store the K elements from the front and K elements from the rear in the res variable.
5. Print the cropped list.

## Python3

 `# Python3 code to demonstrate``# Retain K Front and Rear elements``# using List Comprehension` `# initializing list``test_list ``=` `[``2``, ``3``, ``5``, ``7``, ``9``, ``10``, ``8``, ``6``]` `# printing original list``print` `(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `2` `# using List Comprehension``# Retain K Front and Rear elements``res ``=` `[test_list[i] ``for` `i ``in` `range``(K)] ``+` `[test_list[i] ``for` `i ``in` `range``(``-``K, ``0``)]`  `# printing result``print` `(``"The cropped list is : "` `+` `str``(res))`

Output
```The original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is : [2, 3, 8, 6]```

Time Complexity: The time complexity of this program is O(N), where N is the size of the input list. The list comprehension iterates over the list twice, once for the front elements and once for the rear elements.

Auxiliary Space Complexity: The auxiliary space complexity of this program is O(K), where K is the number of elements to be retained from the front and rear. We store these K elements in the res list.

Method#4:Using enumerate()

Algorithm:

1. Initialize the input list test_list and print it.
2. Initialize the value of K.
3. Use list comprehension with enumerate to retain the first K elements and the last K elements of the list.
4. Print the resultant cropped list.

## Python3

 `# initializing list``test_list ``=` `[``2``, ``3``, ``5``, ``7``, ``9``, ``10``, ``8``, ``6``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `2` `# using List Comprehension and enumerate``res ``=` `[val ``for` `idx, val ``in` `enumerate``(test_list) ``if` `idx < K ``or` `idx >``=` `len``(test_list) ``-` `K]` `# printing result``print``(``"The cropped list is : "` `+` `str``(res))``#This code is contributed by Vinay Pinjala.`

Output
```The original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is : [2, 3, 8, 6]```

Time complexity: O(N), where N is the length of the input list. This is because the code performs a list comprehension operation twice, once for the front K elements and once for the rear K elements.
Auxiliary space: O(N), as a new list is created to store the cropped list. The size of the new list is equal to the sum of the front K elements and rear K elements, which can be at most 2K. However, as K is a constant value, we can consider the space complexity to be O(N) where N is the length of the input list.

Method #5 : Using slicing and extend() method

Approach

1. Slice the list from 0 to K
2. Slice the list from – K to end
3. Concatenate two lists using extend() method
4. Display the extended list

## Python3

 `# Python3 code to demonstrate``# Retain K Front and Rear elements` `# initializing list``test_list ``=` `[``2``, ``3``, ``5``, ``7``, ``9``, ``10``, ``8``, ``6``]` `# printing original list``print` `(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `2`  `# Retain K Front and Rear elements``x``=``test_list[:K]``y``=``test_list[``-``K:]``x.extend(y)``# printing result``print` `(``"The cropped list is : "` `+` `str``(x))`

Output
```The original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is : [2, 3, 8, 6]```

Time Complexity : O(N), where N is the size of the list
Auxiliary space : O(N), where N is the size of the list

Method #6: Using a loop to iterate over the list and appending elements to a new list

## Python

 `# Python3 code to demonstrate``# Retain K Front and Rear elements` `# initializing list``test_list ``=` `[``2``, ``3``, ``5``, ``7``, ``9``, ``10``, ``8``, ``6``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `2` `# Retain K Front and Rear elements``cropped_list ``=` `[]``for` `i ``in` `range``(K):``    ``cropped_list.append(test_list[i])``for` `i ``in` `range``(``-``K, ``0``):``    ``cropped_list.append(test_list[i])` `# printing result``print``(``"The cropped list is : "` `+` `str``(cropped_list))`

Output
```The original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is : [2, 3, 8, 6]```

Time complexity: O(K)
Auxiliary space: O(K) (for the cropped_list list)

Method #7: Using  reduce():

Algorithm:

1. Initialize the original list test_list and K value.
2. Use the reduce() function to iterate over the test_list and reduce the list based on the condition of whether an element is within the first K elements or the last K elements.
3. For each element i, check if it is present in the first K elements of the list or in the last K elements of the list.
4. If i is present in either of the above-mentioned conditions, then add it to the accumulator.
5. Return the final accumulated list as res.
6. Print the final cropped list.

## Python3

 `from` `functools ``import` `reduce` `test_list ``=` `[``2``, ``3``, ``5``, ``7``, ``9``, ``10``, ``8``, ``6``]``# printing original list``print``(``"The original list is : "` `+` `str``(test_list))``K ``=` `2` `res ``=` `reduce``(``lambda` `acc, i: acc ``+` `[i] ``if` `i ``in` `test_list[:K]``             ``or` `i ``in` `test_list[``-``K:] ``else` `acc, test_list, [])` `print``(``"The cropped list is : "` `+` `str``(res))` `# This code is contributed by Rayudu.`

Output
```The original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is : [2, 3, 8, 6]```

Time complexity: O(n)

The reduce() function iterates over the test_list once, which takes O(n) time complexity.
The time complexity of checking if an element is present in a list is O(n).
Thus, the time complexity of the entire code is O(n^2).

Auxiliary Space: O(n):

The space complexity of this code is O(n) as we are creating an accumulator list to store the cropped list.
The size of the accumulator list could be up to n, where n is the size of the original list.

Previous
Next