Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Print Python list elements in circular range
The list data structure in Python holds elements of different data types like integers, strings, or float numbers. Printing list elements in a circular range means starting from any index and wrapping around to the beginning when reaching the end, creating sublists of a specified length.
What is Circular Range?
In a circular range, if you have a list [5, 6, 7, 8] and want sublists of length 4 starting from each position, you get:
Starting from index 0:
[5, 6, 7, 8]Starting from index 1:
[6, 7, 8, 5](wraps around)Starting from index 2:
[7, 8, 5, 6]Starting from index 3:
[8, 5, 6, 7]
Method 1: Using Recursion
This approach uses a recursive function to generate circular sublists starting from each position ?
def circular_range(numbers, k, start_index=0):
# Base case: if we've processed all starting positions
if start_index == len(numbers):
return
# Create sublist of length k starting from start_index
sublist = []
for i in range(k):
# Use modulo to wrap around to beginning
sublist.append(numbers[(start_index + i) % len(numbers)])
print(sublist)
# Recursive call for next starting position
circular_range(numbers, k, start_index + 1)
# Example usage
numbers = [5, 6, 7, 8]
k = 4
circular_range(numbers, k)
[5, 6, 7, 8] [6, 7, 8, 5] [7, 8, 5, 6] [8, 5, 6, 7]
Method 2: Using Iteration
This approach uses nested loops to generate all circular sublists ?
def print_circular_range(numbers, k):
# Iterate through each possible starting position
for start in range(len(numbers)):
sublist = []
# Generate k elements starting from current position
for i in range(k):
# Use modulo operator to wrap around
sublist.append(numbers[(start + i) % len(numbers)])
print(sublist)
# Example usage
numbers = [5, 6, 7, 8]
k = 4
print_circular_range(numbers, k)
[5, 6, 7, 8] [6, 7, 8, 5] [7, 8, 5, 6] [8, 5, 6, 7]
Method 3: Using itertools.cycle
Python's itertools.cycle creates an infinite iterator that cycles through the list elements ?
from itertools import cycle, islice
def circular_range_cycle(numbers, k):
for start in range(len(numbers)):
# Create a cyclic iterator starting from current position
cyclic_list = cycle(numbers[start:] + numbers[:start])
# Take k elements from the cyclic iterator
sublist = list(islice(cyclic_list, k))
print(sublist)
# Example usage
numbers = [5, 6, 7, 8]
k = 4
circular_range_cycle(numbers, k)
[5, 6, 7, 8] [6, 7, 8, 5] [7, 8, 5, 6] [8, 5, 6, 7]
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Recursion | O(n × k) | O(n) call stack | Learning recursion |
| Iteration | O(n × k) | O(k) | Simple, readable code |
| itertools.cycle | O(n × k) | O(n + k) | Memory-efficient for large k |
Practical Example
Here's a practical example with different sublist lengths ?
def demonstrate_circular_range():
colors = ['red', 'blue', 'green']
print("Circular range with k=2:")
for start in range(len(colors)):
sublist = []
for i in range(2):
sublist.append(colors[(start + i) % len(colors)])
print(f"Starting from {colors[start]}: {sublist}")
demonstrate_circular_range()
Circular range with k=2: Starting from red: ['red', 'blue'] Starting from blue: ['blue', 'green'] Starting from green: ['green', 'red']
Conclusion
Circular range printing allows you to generate all possible sublists of a given length by wrapping around the original list. Use the iteration method for simplicity, recursion for educational purposes, or itertools.cycle for memory efficiency with large datasets.
