Skip to content

Latest commit

 

History

History
123 lines (99 loc) · 3.4 KB

File metadata and controls

123 lines (99 loc) · 3.4 KB

Python List Reversal Documentation

Python List Reversal ([::-1], reverse(), reversed())

In Python, :: is a slicing notation used for sequences (lists, tuples, and strings). The general slicing syntax is:

list[start:stop:step]

Breakdown of slicing parameters:

  • start: Starting index (inclusive, default is 0).
  • stop: Ending index (exclusive, default is len(list)).
  • step: Step size (default is 1).

When using ::, both start and stop are omitted, and only step is considered.


Examples of List Slicing

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Using [::] (equivalent to [::1], meaning the whole list)
print(my_list[::])  
# Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Every second element
print(my_list[::2])  
# Output: [0, 2, 4, 6, 8]

# Reverse the list
print(my_list[::-1])  
# Output: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

# Every second element in reverse order
print(my_list[::-2])  
# Output: [9, 7, 5, 3, 1]

:: is mostly used for stepping or reversing sequences in Python.


Different Ways to Reverse a List in Python

Method 1: Using Slicing ([::-1])

my_list = [1, 2, 3, 4, 5]
reversed_list = my_list[::-1]
print("Reversed List using slicing:", reversed_list)

Output:

Reversed List using slicing: [5, 4, 3, 2, 1]

✔ Creates a new list while keeping the original list unchanged.


Method 2: Using reverse() (In-Place)

my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print("Reversed List using reverse():", my_list)

Output:

Reversed List using reverse(): [5, 4, 3, 2, 1]

Modifies the original list (does not create a new one).
Efficient as it does not use extra memory.


Method 3: Using reversed() (Iterator)

my_list = [1, 2, 3, 4, 5]
reversed_list = list(reversed(my_list))
print("Reversed List using reversed():", reversed_list)

Output:

Reversed List using reversed(): [5, 4, 3, 2, 1]

Creates a new list while keeping the original list unchanged.
✔ Uses an iterator, making it memory-efficient for large lists.


Method 4: Using a Loop

my_list = [1, 2, 3, 4, 5]
reversed_list = []
for item in my_list[::-1]:  # Iterating in reverse order
    reversed_list.append(item)
print("Reversed List using loop:", reversed_list)

Output:

Reversed List using loop: [5, 4, 3, 2, 1]

Manually appends elements in reverse order.
Not efficient compared to built-in methods.


Comparison of Methods

Method Modifies Original List? Creates New List?
reverse() ✅ Yes ❌ No
[::-1] ❌ No ✅ Yes
reversed() ❌ No ✅ Yes

When to Use Which?

  • Use reverse() if you want to modify the original list in-place.
  • Use [::-1] or reversed() if you need a new reversed list while keeping the original unchanged.

🚀 Best Practice:

  • [::-1] is best for quick list reversal.
  • reverse() is best for modifying the list without extra memory usage.
  • reversed() is best when dealing with large lists since it returns an iterator.

Now you can confidently reverse lists in Python using the best method for your needs! 🎯