How to swap elements in an array in Python, and how to reverse an array.

I will write how to exchange elements in an array in Python and how to reverse the array. Thank you.

n = [*range(1,6)]
for i in range(len(n)//2):
    n[i], n[len(n)-i-1] = [len(n)-i-1], n[i]
#The beginning and the back, the second and the second from the back are exchanged.

If the goal is to reverse the array.

n = [*range(1,6)][::-1]
n = [*range(1,6)]
reverse_n = sorted(n,reverse=True)
#sorted is non-destructive, does not change n itself, and returns n in reverse order.
n = [*range(1,6)]
n.sort(reverse=True)
#sort is destructive and modifies n itself. The value returned is None.

that's all. Thank you very much.

Recommended Posts

How to swap elements in an array in Python, and how to reverse an array.
[Python] How to swap array values
How to use is and == in Python
How to make a string into an array or an array into a string in Python
How to generate permutations in Python and C ++
How to plot autocorrelation and partial autocorrelation in python
How to remove duplicate elements in Python3 list
Extract every n elements from an array (list) in Python and Ruby
Quicksort an array in Python 3
How to develop in Python
[Python] How to sort dict in list and instance in list
How to create an image uploader in Bottle (Python)
[Python] How to do PCA in Python
How to slice a block multiple array from a multiple array in Python
How to convert an array to a dictionary with Python [Application]
How to collect images in Python
[python] Summary of how to retrieve lists and dictionary elements
How to use SQLite in Python
[Python] How to write an if statement in one sentence.
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Comparison of how to use higher-order functions in Python 2 and 3
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
Reverse Hiragana and Katakana in Python2.7
How to execute external shell scripts and commands in python
How to create dataframes and mess with elements in pandas
Data science companion in python, how to specify elements in pandas
How to handle Japanese in Python
An alternative to `pause` in Python
How to log in to AtCoder with Python and submit automatically
How to install OpenCV on Cloud9 and run it in Python
How to know the internal structure of an object in Python
How to compare lists and retrieve common elements in a list
How to use functions in separate files Perl and Python versions
How to create a heatmap with an arbitrary domain in Python
Difference in how to write if statement between ruby ​​and python
[ROS2] How to describe remap and parameter in python format launch
Various ways to create an array of numbers from 1 to 10 in Python.
How to display bytes in the same way in Java and Python
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
How to install and use pandas_datareader [Python]
How to do R chartr () in Python
python3 How to install an external module
How to convert Python to an exe file
[Itertools.permutations] How to put permutations in Python
How to work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
python: How to use locals () and globals ()
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
[Python] How to calculate MAE and RMSE
How to use Python zip and enumerate