Reverse
In [1]:
In [2]:
#Reverse a List of Integers
integers = [1, 2, 3, 4, 5]
integers.reverse()
print(integers)
integers = [1, 2, 3, 4, 5]
reversed_integers = integers[::-1]
print(reversed_integers)
[5, 4, 3, 2, 1] [5, 4, 3, 2, 1]
In [3]:
#Reverse a List of Strings
strings = ["apple", "banana", "cherry"]
strings.reverse()
print(strings)
strings = ["apple", "banana", "cherry"]
reversed_strings = strings[::-1]
print(reversed_strings)
['cherry', 'banana', 'apple'] ['cherry', 'banana', 'apple']
In [4]:
#Reverse a List of Dictionaries
dicts = [{"name": "Alice"}, {"name": "Bob"}, {"name": "Charlie"}]
dicts.reverse()
print(dicts)
dicts = [{"name": "Alice"}, {"name": "Bob"}, {"name": "Charlie"}]
reversed_dicts = dicts[::-1]
print(reversed_dicts)
[{'name': 'Charlie'}, {'name': 'Bob'}, {'name': 'Alice'}]
[{'name': 'Charlie'}, {'name': 'Bob'}, {'name': 'Alice'}]
In [5]:
import pprint
#Reverse a List Using Healthcare Data
patients = [
{"id": 1, "name": "John Doe", "age": 30, "condition": "Flu"},
{"id": 2, "name": "Jane Smith", "age": 25, "condition": "Cold"},
{"id": 3, "name": "Emily Davis", "age": 40, "condition": "Asthma"}
]
patients.reverse()
pprint.pp(patients)
patients = [
{"id": 1, "name": "John Doe", "age": 30, "condition": "Flu"},
{"id": 2, "name": "Jane Smith", "age": 25, "condition": "Cold"},
{"id": 3, "name": "Emily Davis", "age": 40, "condition": "Asthma"}
]
reversed_patients = patients[::-1]
pprint.pp(reversed_patients)
[{'id': 3, 'name': 'Emily Davis', 'age': 40, 'condition': 'Asthma'},
{'id': 2, 'name': 'Jane Smith', 'age': 25, 'condition': 'Cold'},
{'id': 1, 'name': 'John Doe', 'age': 30, 'condition': 'Flu'}]
[{'id': 3, 'name': 'Emily Davis', 'age': 40, 'condition': 'Asthma'},
{'id': 2, 'name': 'Jane Smith', 'age': 25, 'condition': 'Cold'},
{'id': 1, 'name': 'John Doe', 'age': 30, 'condition': 'Flu'}]
