GeeksforGeeks App
Open App
Browser
Continue

# Python – Alternate Minimum element in list

Some of the list operations are quite general and having shorthands without needing to formulate a multiline code is always required. Wanting to construct the list consisting of all the alternate elements of the original list is a problem that one developer faces in day-day applications and sometimes require to find minimum of these alternate elements. Let’s discuss certain ways in which this can be performed.

Method #1 : Using list comprehension + min()
Shorthand to the naive method, list comprehension provides a faster way to perform this particular task. In this method, all the indices which are not multiple of 2, hence odd are inserted in the new list. Then the minimum is extracted using min().

## Python3

 `# Python code to demonstrate``# Alternate elements Minimum``# using list comprehension + min()` `# initializing list``test_list ``=` `[``1``, ``4``, ``6``, ``7``, ``9``, ``3``, ``5``]` `# printing original list``print` `(``"The original list : "` `+` `str``(test_list))` `# using list comprehension + min()``# Alternate elements Minimum``res ``=` `min``([test_list[i] ``for` `i ``in` `range``(``len``(test_list)) ``if` `i ``%` `2` `!``=` `0``])` `# printing result``print` `(``"The alternate element list minimum is : "` `+` `str``(res))`

Output :

```The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element list minimum is : 3```

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method #2 : Using enumerate() + min()
This is just a variation to the list comprehension method but does the similar internal working as list comprehension but uses different variables to keep track of index along with its value. Then the minimum is extracted using min().

## Python3

 `# Python code to demonstrate``# Alternate elements Minimum``# using enumerate() + min()` `# initializing list``test_list ``=` `[``1``, ``4``, ``6``, ``7``, ``9``, ``3``, ``5``]` `# printing original list``print` `(``"The original list : "` `+` `str``(test_list))` `# using enumerate() + min()``# Alternate elements Minimum``res ``=` `min``([i ``for` `j, i ``in` `enumerate``(test_list) ``if` `j ``%` `2` `!``=` `0``])` `# printing result``print` `(``"The alternate element list minimum is : "` `+` `str``(res))`

Output :

```The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element list minimum is : 3```

Time Complexity: O(n) where n is the number of elements in the string list. The enumerate() + min()  is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the test list.

Method #3 : Using slice + min()
In this method, we slice the list to get alternate elements and min() to get the minimum of alternate elements.

## Python3

 `# Python code to demonstrate``# Alternate elements Minimum``# using slice + min()` `# initializing list``test_list ``=` `[``1``, ``4``, ``6``, ``7``, ``9``, ``3``, ``5``]` `# printing original list``print` `(``"The original list : "` `+` `str``(test_list))` `# using slice + min()``# Alternate elements Minimum``res ``=` `min``(test_list[``1``::``2``])` `# printing result``print` `(``"The alternate element list minimum is : "` `+` `str``(res))` `#This code is contributed by Edula Vinay Kumar Reddy`

Output

```The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element list minimum is : 3```

Time Complexity: O(n) for all the methods as all the methods traverse the list once to get the minimum of alternate elements.

Auxiliary Space: O(1) for all the methods as all the methods only use a constant amount of extra space.

Method #4 : Using the reduce() function from the functools module:

1. Import the reduce() function from the functools module
2. Define the test_list as the input list
3. Use slicing to get the alternate elements from the list starting at index 1
4. Use the reduce() function with a lambda function as the first argument to compare the elements and return the minimum value
5. Store the minimum value in the min_num variable
6. Print the result

## Python3

 `from` `functools ``import` `reduce``# initializing list``test_list ``=` `[``1``, ``4``, ``6``, ``7``, ``9``, ``3``, ``5``]``# printing original list``print` `(``"The original list : "` `+` `str``(test_list))`` ` `min_num ``=` `reduce``(``lambda` `a, b: a ``if` `a < b ``else` `b, test_list[``1``::``2``])``# printing result``print``(``"The alternate element list minimum is:"``, min_num)``#This code is contributed by Jyothi pinjala.`

Output

```The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element list minimum is: 3```

The time complexity :  O(n) because the reduce() function iterates over half of the input list to compare and reduce the values to a single minimum value.

The space complexity : O(1) because we are not using any additional data structures to store intermediate values or the input list.

My Personal Notes arrow_drop_up