How to use list []

What is a list type []?

a=[1,2,3]
b=["a","b","c"]

Something like that.

List type advantages

  1. Can handle a lot of data at once
  2. Various operations such as adding / deleting elements and searching are possible.

Add an element

a=[1,2,3]
#.append()
a.ppend(4)
print(a)

Execution result


[1,2,3,4]

Add elements using append like this

Delete the element

a=[1,2,3,4]
#.remove()
a.remove(4)
print(a)

Execution result


[1,2,3]

Use remove like this to remove an element

Search for elements

a=[1,2,3]
print(a[0])
print(a[1])
print(a[2])

Execution result


1
2
3

The elements of the list are 0,1,2, ... from the left What happens from the right?

a=[1,2,3]
print([-1])
print([-2])
print([-3])

Execution result


3
2
1

The elements of the list are -1, -2, -3, ... from the right.

Search for multiple elements

a=[1,2,3,4,5,6]
#1~Extract 3 elements
print(a[0:3])
#2~Extract 6 elements
print(a[1:])
#1~Take out every two elements of 6
print([::2])

Execution result


[1,2,3]
[2,3,4,5,6]
[1,3,5]

Advanced version

#There is a list in the list
a=[[1,2],[3,4],[5,6]]
print(a[0])
print(a[0][0])

text:Execution result:


#In the list of a[1,2]Taken out
[1,2]
#In the list of a[1,2]I took out the first one from the left of
1

Recommended Posts

How to use list []
[Python] How to use list 1
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use Pandas 2
How to use Virtualenv
How to use pytest_report_header
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
Summary of how to use Python list
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
How to use FastAPI ③ OpenAPI
How to use IPython Notebook
How to use Pandas Rolling
[Note] How to use virtualenv
Python: How to use pydub
[Python] How to use checkio
[Algorithm x Python] How to use the list
How to use Django's GeoIp2
[Python] How to use input ()
How to use the decorator
[Introduction] How to use open3d
How to use Python lambda
How to use Jupyter Notebook
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Google Colaboratory
How to use Python bytes
How to use cron (personal memo)
Python: How to use async with
How to use the zip function
How to use the optparse module
How to use SWIG from waf