Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
ffd6bfae
Commit
ffd6bfae
authored
Nov 12, 2003
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Explain the advantages of reversed.
parent
03f18e58
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
3 deletions
+6
-3
Doc/whatsnew/whatsnew24.tex
Doc/whatsnew/whatsnew24.tex
+6
-3
No files found.
Doc/whatsnew/whatsnew24.tex
View file @
ffd6bfae
...
...
@@ -34,7 +34,7 @@ and returns an iterator that returns the elements of the sequence
in reverse order.
\begin{verbatim}
>>> for i in reversed(
[1,2,3]
):
>>> for i in reversed(
xrange(1,4)
):
... print i
...
3
...
...
@@ -42,9 +42,12 @@ in reverse order.
1
\end{verbatim}
Compared to extended slicing,
\code
{
range(1,4)[::-1]
}
,
\function
{
reversed()
}
is easier to read, runs faster, and uses substantially less memory.
Note that
\function
{
reversed()
}
only accepts sequences, not arbitrary
iterators. If you want to reverse an iterator, convert it to
a list
or tuple with
\function
{
list()
}
or
\function
{
tuple
()
}
.
iterators. If you want to reverse an iterator,
first
convert it to
a list
with
\function
{
list
()
}
.
\begin{verbatim}
>>> input = open('/etc/passwd', 'r')
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment