DekGenius.com
PYTHON
how to reverse a list in python
mylist
[1, 2, 3, 4, 5]
mylist[::-1]
[5, 4, 3, 2, 1]
how to reverse a list in python
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
reverse element in a list in python 3
my_list = [1, 7, 9, 11, 12, 20]
# Reverse a list by using a slice
print(my_list[::-1])
how to reverse a list in python
# reversing a list in Python
numbers = [1,2,3,4,5,6,7,8,9]
print(numbers[::-1])
how to reverse a list in python
list = [1,2,3,4,5]
reversed_list = list[::-1] # [::-1] [start : stop : step]
print(reversed_list)
how to reverse a list in python
lst = [1, 2, 3]
reversed_lst = reversed(lst)
how to reverse list python
>>> xs = [0, 10, 20, 40]
>>> for i in reversed(xs):
... print(i)
reverse the order of list elements
# create a list of prime numbers
prime_numbers = [2, 3, 5, 7]
# reverse the order of list elements
prime_numbers.reverse()
print('Reversed List:', prime_numbers)
# Output: Reversed List: [7, 5, 3, 2]
how to reverse a list in python
© 2022 Copyright:
DekGenius.com