Print Python list elements in circular range


The list data structure holds elements of different data types like integers, strings, or float numbers. The elements that are once defined inside the list data structure that is within the square brackets cannot be changed. To print the list data structure in a circular range, the Python language provides the user with some inbuilt functionalities. So inside the list, we can store multiple values together and thus reducing the complex operations. Python works on the OOPS concept and is popular among programmers due to its simpler code.

Print Python list elements in circular range

As the list data structure is composed of various elements in some particular range. If the list consists of 10 elements that are from 0 to 9, then print the elements in the list in the circular range from some starting point to a particular ending point. The user can also provide the number of iterations it has to undergo in the functionalities.

Approach

  • Approach 1 − Using the recursion function

  • Approach 2 − Using the cycle module

Approach 1: Python Program to print list elements in circular range using recursion function

We define a new function called circular_range that takes three arguments: list1, k, and a. The ‘a’ argument is optional and defaults to zero and this argument is used to keep track of the current index position within list1.

Algorithm

  • Step 1 − The function is defined with three parameters as list1, integer first, and second.

  • Step 2 − Create an empty list.

  • Step 3 − If the current index position ‘a’ is equal to the length of list1, exit the function.

  • Step 4 − Use a nested loop to iterate over the next k elements in list1.

  • Step 5 − Append each element to ans, taking care to wrap around to the beginning of the list using the modulo operator (%).

  • Step 6 − Print out the resulting sublist

  • Step 7 − Call the circular_range function again with an updated value for a.

  • Step 8 − Repeat steps 2-7 until all sublists of length k have been generated and the list is defined with values ranging from 1 to 4.

  • Step 9 − Two integers are defined and initialized with the values.

  • Step 10 − The for loop will iterate through the list.

  • Step 11 − All the elements in a group of 4 are returned and Finally, we print out the resulting sublist and call the circular_range function again with an updated value for a.

Example

#defining the function with three parameters
def circular_range(list1, k, a=0):
    #To check whether the current index value element is equal to the length of the list
    if a == len(list1):
        return
    #creating an empty list
    ans = []
    #nested for loop is used to iterate through the list for the next element
    for b in range(k):
        #Adding the element at the beginning of the list using modulo(%) operator
        ans.append(list1[(a+b)%len(list1)])
    #prints the answer    
    print(ans)
    #calling the function recursively for all the elements
#The function uses recursion to generate each sublist of length k
    circular_range(list1, k, a+1)
#initializing the list with a set of values
list1 = [5, 6, 7, 8]
k = 4
#the function is repeated all the possible elements are printed
circular_range(list1, k)

Output

[5, 6, 7, 8]
[6, 7, 8, 5]
[7, 8, 5, 6]
[8, 5, 6, 7]

Approach 2: Python Program to print list elements in circular range using the iteration method

For printing the items in the list in a circular manner for all ranges of possibilities, the iteration method is used.

Algorithm

  • Step 1 − The list is created with a range of elements.

  • Step 2 − The variable is set to 4.

  • Step 3 − The for loop is used to iterate through the list according to the range.

  • Step 4 − The new list is created to hold the list of circular ranges

  • Step 5 − The nested for loop is used to iterate through the range of the k value.

  • Step 6 − The final list of values in the circular range is printed in all possible ranges.

Example

#creating the list with integer elements
list1 = [5, 6, 7, 8]
#declaring the variable k as 4
k = 4
#iteration through the list using the len function
for a in range(len(list1)):
#initializing the empty list
    ans = []
#nested for loop to iterate using the “k”
    for b in range(k):
        ans.append(list1[(a+b)%len(list1)])
#final list with a circular range is printed
    print(ans)

Output

[5, 6, 7, 8]
[6, 7, 8, 5]
[7, 8, 5, 6]
[8, 5, 6, 7]

Conclusion

Python is used in a wide range of technologies like machine learning and artificial intelligence. The approaches help the programmer print all the elements in the list in a circular range. Data handling is the most prominent one in this current technology and printing element in a circular range is one of the fundamental functions.

Updated on: 04-Sep-2023

110 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements