In Python, :: is a slicing notation used for sequences (lists, tuples, and strings). The general slicing syntax is:
list[start:stop:step]start: Starting index (inclusive, default is0).stop: Ending index (exclusive, default islen(list)).step: Step size (default is1).
When using ::, both start and stop are omitted, and only step is considered.
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.
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.
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.
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.
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.
| Method | Modifies Original List? | Creates New List? |
|---|---|---|
reverse() |
✅ Yes | ❌ No |
[::-1] |
❌ No | ✅ Yes |
reversed() |
❌ No | ✅ Yes |
- Use
reverse()if you want to modify the original list in-place. - Use
[::-1]orreversed()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! 🎯