9 examples of 'reverse a list in python' in Python

Every line of 'reverse a list in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
45def reverse(l):
46 """Reverse list"""
47 return list(reversed(l))
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

46def reverse(list_node: ListNode) -> ListNode:
47 """Reverse a list."""
48 current = list_node
49 previous = None
50
51 while current is not None:
52 previous, current.next, current = current, previous, current.next
53
54 return previous
393def reverse(self):
394 self._values.reverse()
50@export_evaluater_function(function_group='basic')
51def reverse(list):
52 """**reverse(list)** -> Returns a list tha is the reverse order of the input
53
54<code>
55 Example:
56
57 reverse( [ 1, 2, 3 ] )
58
59 Returns:
60
61 [3, 2, 1]
62
63</code>
64
65 """
66 n_list = copy.copy(list)
67 n_list.reverse()
68 return n_list
78def reverse(self):
79 is_immutable(self)
353def __reversed__(self):
354 current = self.__last
355 while current is not None:
356 yield current.value
357 current = current.prev
86def reverse_all(l):
87 """
88 Reverse a list, while reversing its content.
89
90 Example:
91 l: ["abcde", "fghij"]
92 result: ["jihgf", "edcba"]
93
94 :param l: List (list)
95 :rtype: Reversed list (list)
96 """
97 output = []
98 for x in l[::-1]:
99 output.append("".join(list(reversed(x))))
100
101 return output
102def reversed(seq):
103 return list(map(seq.__getitem__, range(len(seq) - 1, -1, -1)))
105def iterative_reverse(self):
106 """ Reverse the whole chain of nodes iteratively. """
107 previous_node = None
108 node = self.root_node
109 while node:
110 next_node = node.next_node
111 node.next_node = previous_node
112 previous_node = node
113 node = next_node
114 self.root_node = previous_node

Related snippets