[Python] How to swap array values

[Python] Swapping arrays

Elements can be easily replaced by connecting the element numbers with equals.

For example, if you want to swap the beginning and end of an array containing integers from 1 to 5, you can swap the values on the left and right and connect them with equals.


a = [1,2,3,4,5]

a[0], a[4] = a[4], a[0]

#[5, 2, 1, 4, 5]

## what are you doing? The process that can input the specified value to the specified value is applied.


a = [1,2,3,4,5]

a[0], a[1], a[2] = a[4], a[4], 5

#[5, 5, 5, 4, 5]

Simple version

It can be used without giving a variable name.


a,b,c,d =1,2,3,4
a,c = c, a

#3 2 1 4

If the numbers do not match

If the value on the left side is other than one, an error will occur. When there is only one on the left side, a set value is entered.


a = [1,2,3,4,5]

a[0] = a[4], a[4], a[4]

#[(5, 5, 5), 2, 3, 4, 5]


a = [1,2,3,4,5]

a[0], a[1] = a[4], a[4], a[4]

#ValueError: too many values to unpack (expected 2)

Recommended Posts

[Python] How to swap array values
How to swap elements in an array in Python, and how to reverse an array.
How to install Python
How to install python
[2020.8 latest] How to install Python
How to install Python [Windows]
python3: How to use bottle (2)
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[Python] How to output the list values in order
How to install python using anaconda
How to write a Python class
[Python] How to FFT mp3 data
[Python] How to do PCA in Python
Python: How to use async with
Subscript access to python numpy array
How to slice a block multiple array from a multiple array in Python
[Python] How to derive nCk (ABC156-D)
[Python] How to use Pandas Series
How to collect images in Python
How to use Requests (Python Library)
How to use SQLite in Python
[Introduction to Python] How to parse JSON
How to transpose a 2D array using only python [Note]
How to get started with Python
[Python] How to import the library
[Python] How to use list 3 Added
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to speed up Python calculations
How to calculate date with python
How to access wikipedia from python
How to use python zip function
[Nanonets] How to post Memo [Python]
How to handle Japanese in Python
[Python] How to use Typetalk API
How to make a string into an array or an array into a string in Python
[Python] How to store a csv file as one-dimensional array data
[Python] How to convert db file to csv
[Python] Summary of how to use pandas
How to package and distribute Python scripts
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to read pydoc on python interpreter