reversed(my_sequence)
my_sequence : object supporting sequence protocol. my_list=['One','Two','Three']
print(my_list) # ['One', 'Two', 'Three']
my_list=list(reversed(my_list))
print(my_list) # ['Three', 'Two', 'One']
my_tuple=('One','Two','Three')
print(my_tuple) # ('One', 'Three', 'Two')
my_tuple=tuple(reversed(my_tuple))
print(my_tuple) # ('Three', 'Two', 'One')
Using a range
my_range=range (6)
for i in my_range:
print(i, end=' ') # 0 1 2 3 4 5
my_range2=reversed(my_range)
for i in my_range2:
print(i, end=' ') # 5 4 3 2 1 0
using a string
my_str='plus2net.com'
print(list(reversed(my_str)))
# ['m', 'o', 'c', '.', 't', 'e', 'n', '2', 's', 'u', 'l', 'p']
str='plus2net'
str_rev=''.join(reversed(str))
print(str_rev)
Output
ten2sulp
my_set = {1, 2, 3, 4}
reversed_set = list(reversed(list(my_set)))
print(reversed_set) # Output: [4, 3, 2, 1]
Output:
[4, 3, 2, 1]
my_list = ['a', 'b', 'c', 'd']
for index, value in enumerate(reversed(my_list)):
print(f'{index}: {value}')
Output: Index with reversed values
0: d
1: c
2: b
3: a
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.