Python – Alternate rear iteration


Introduction

Implementing alternate rear iteration using Python involves basic usage of list data structure and functionality with the help of looping structures. This problem applies simple mathematical skills that allow us to determine even numbers to print the alternate number from the back side. The data structures available in the Python language are lists, tuples, and dictionaries. It provides the users with the facility of iterating through the different types of data structures. Most commonly, the for loop is used to iterate through the lists or tuples.

Alternate rear iteration

In this article, the iteration is followed from the back or rear of the given list and is more efficient when compared to the iteration from the front.

Approach

Approach 1 − Using for loop

Approach 2 − Using slicing method

Approach 3 − Using lambda function

Approach 1: Python Program to alternate rear iteration using for loop

The range() function is declared with three arguments and they are the length of the given as len(list_1)-1, which indicates the last index of the list, the second argument is -1 which indicates iteration should not move beyond the beginning and the last argument is -1 which indicates that we have to move backward or rear.

Algorithm

  • Step 1 − The list is initialized with integer elements.

  • Step 2 − Initialize the empty list.

  • Step 3 − The for loop is used to iterate through every element of the list from the rear.

  • Step 4 − The length of the list is identified using the len() function and the range of the elements is maintained using the range() function.

  • Step 5 − Then the print statement is used to return the list of elements after the rear iteration.

Example

#initializing the list with integer elements
list_1 = [1,2,3,4,5,6,8]
#initializing the empty list
alternate_list = []
#for loop is used to iterate through the loop from the rear using the range() and len() function
for a in range(len(list_1)-1,-1,-2):
   alternate_list.append(list_1[a])
#returns the elements of the list iterated from the rear
print("Alternate rear iteration:", alternate_list)

Output

Alternate rear iteration: [8, 5, 3, 1]

Approach 2: Python Program to alternate rear iteration using slicing method

The slicing method is used to move through the string with a step size of -1. Instead of moving from the left to right iteration, this method is followed.

Algorithm

  • Step 1 − The user is prompted to enter the values and With the help of split() function, the entered integer is split into separate elements.

  • Step 2 − The given string simply undergoes the slicing method to reverse the given string.

  • Step 3 − Then the print statement is used to return the reversed characters of the string.

Example

#The user is prompted to enter the integers with proper spaces
#split() function is used to split the given strings into separate ones
list1 = [1, 2, 3, 5, 7, 9, 4]
list1 = [int(a) for a in list1]

#The list of elements given by the user and the string is reversed using the slicing method
alternate_list = list(reversed(list1[::2]))
#printing statement will return the given input in a reversed alternate order
print("Alternate rear iteration:", alternate_list)

Output

Alternate rear iteration: [4, 7, 3, 1]

Approach 3: Python Program to alternate rear iteration using lambda function

In the case of a function, we use def function but for the anonymous function, we can use the lambda function along with the key parameter. The lambda function usually comes along with either filter() or map() function.

Algorithm

  • Step 1 − The user is prompted to enter the integer values along with the spaces and stored them in the variable named list1.

  • Step 2 − With the help of split() function, the entered integer is split into separate elements.

  • Step 3 − The given strings of values are converted into integer data type using the list comprehension.

  • Step 4 − As the filter function always comes with the lambda function, it filters the given list of elements from the rear side.

  • Step 5 − The key parameter “m†is used to check whether it is an even number or not.

  • Step 6 − Later, using the reversed() function, the converted input string is reversed for the process of rear iteration.

  • Step 7 − Then the print statement is used to return the list of elements after the rear iteration.

Example

#The user is prompted to enter the integers with proper spaces
#split() function is used to split the given strings into separate ones
list1 = [1, 2, 3, 5, 7, 9, 4]
list1 = [int(a) for a in list1]

#lambda function is used to get the alternate rear iteration using filter() and map() function
alternate_list = list(filter(lambda m: list1.index(m) % 2 == 0, reversed(list1)))
#printing statement will return the given input in a reversed alternate order
print("Alternate rear iteration:", alternate_list)

Output

Alternate rear iteration: [4, 7, 3, 1]

Conclusion

In the current world, handling data is the most challenging task for organizations with a high volume of data, and with the development of data science and machine learning it has become easier to access. Python is a versatile and high-level language that can be understood easily by the user.

Updated on: 25-Aug-2023

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements