Questions tagged [reverse]

Rearranging the order of a sequence such that the final order is a mirror image of the original.

Rearranging the order of a sequence such that the final order is a mirror image of the original.

3773 questions
1194
votes
9 answers

How do I sort a list of objects based on an attribute of the objects?

I have a list of Python objects that I want to sort by a specific attribute of each object: [Tag(name="toe", count=10), Tag(name="leg", count=2), ...] How do I sort the list by .count in descending order?
Nick Sergeant
  • 35,843
  • 12
  • 36
  • 44
1036
votes
28 answers

Traverse a list in reverse order in Python

How do I traverse a list in reverse order in Python? So I can start from collection[len(collection)-1] and end in collection[0]. I also want to be able to access the loop index.
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
1017
votes
33 answers

Reverse / invert a dictionary mapping

Given a dictionary like so: my_map = {'a': 1, 'b': 2} How can one invert this map to get: inv_map = {1: 'a', 2: 'b'}
Brian M. Hunt
  • 81,008
  • 74
  • 230
  • 343
579
votes
56 answers

How do you reverse a string in-place in JavaScript?

How do you reverse a string in-place in JavaScript when it is passed to a function with a return statement, without using built-in functions (.reverse(), .charAt() etc.)?
Jake
508
votes
20 answers

Print a list in reverse order with range()?

How can you produce the following list with range() in Python? [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
ramesh.mimit
  • 9,445
  • 4
  • 21
  • 23
472
votes
12 answers

JQuery .each() backwards

I'm using JQuery to select some elements on a page and then move them around in the DOM. The problem I'm having is I need to select all the elements in the reverse order that JQuery naturally wants to select them. For example:
  • Item…
Jack Mills
  • 6,022
  • 5
  • 34
  • 38
454
votes
7 answers

Python list sort in descending order

How can I sort this list in descending order? timestamps = [ "2010-04-20 10:07:30", "2010-04-20 10:07:38", "2010-04-20 10:07:52", "2010-04-20 10:08:22", "2010-04-20 10:08:22", "2010-04-20 10:09:46", "2010-04-20…
Rajeev
  • 44,985
  • 76
  • 186
  • 285
245
votes
12 answers

How to get a reversed list view on a list in Java?

I want to have a reversed list view on a list (in a similar way than List#sublist provides a sublist view on a list). Is there some function which provides this functionality? I don't want to make any sort of copy of the list nor modify the list. It…
Albert
  • 65,406
  • 61
  • 242
  • 386
240
votes
7 answers

Right way to reverse a pandas DataFrame?

Here is my code: import pandas as pd data = pd.DataFrame({'Odd':[1,3,5,6,7,9], 'Even':[0,2,4,6,8,10]}) for i in reversed(data): print(data['Odd'], data['Even']) When I run this code, i get the following error: Traceback (most recent call…
Michael
  • 15,386
  • 36
  • 94
  • 143
231
votes
17 answers

angular ng-repeat in reverse

How can i get a reversed array in angular? i'm trying to use orderBy filter, but it needs a predicate(e.g. 'name') to sort: {{friend.name}} {{friend.phone}} …
Delremm
  • 3,121
  • 4
  • 19
  • 13
193
votes
21 answers

How do you reverse a string in place in C or C++?

How do you reverse a string in C or C++ without requiring a separate buffer to hold the reversed string?
uvote
170
votes
15 answers

Can one do a for each loop in java in reverse order?

I need to run through a List in reverse order using Java. So where this does it forwards: for(String string: stringList){ //...do something } Is there some way to iterate the stringList in reverse order using the for each syntax? For clarity: I…
Ron Tuffin
  • 53,859
  • 24
  • 66
  • 78
169
votes
21 answers

How to read a file in reverse order?

How to read a file in reverse order using python? I want to read a file from last line to first line.
Nimmy
  • 5,051
  • 7
  • 33
  • 32
150
votes
35 answers

How to reverse a string in Go?

How can we reverse a simple string in Go?
user211499
  • 1,537
  • 2
  • 11
  • 6
148
votes
19 answers

How do I exchange keys with values in a dictionary?

I receive a dictionary as input, and would like to to return a dictionary whose keys will be the input's values and whose value will be the corresponding input keys. Values are unique. For example, say my input is: a =…
Roee Adler
  • 33,434
  • 32
  • 105
  • 133
1
2 3
99 100