Is this possible? Doesn’t have to be in place, just looking for a way to reverse a tuple so I can iterate on it backwards.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are two idiomatic ways to do this:
or
Since tuples are immutable, there is no way to reverse a tuple in-place.
Edit:
Building on @lvc’s comment, the iterator returned by
reversedwould be equivalent toi.e. it relies on the sequence having a known length to avoid having to actually reverse the tuple.
As to which is more efficient, i’d suspect it’d be the
seq[::-1]if you are using all of it and the tuple is small, andreversedwhen the tuple is large, but performance in python is often surprising so measure it!