Python basics: list

List type

A variable contains multiple data.

List basics

Character strings and numerical values can be mixed

a = 1
b = 'Mandarin orange'
c = 2

li = [a, b, c]

print(li)
#output[1,Mandarin orange,2]

Multiple list

List in list



a = "Ichi"
b = 2
c = "Mr."
d = 4

li = [[a, b], [c, d]]


print(li)
#output: [["Ichi", 2], ["Mr.", 4]]

Price withdrawal

Indexes (numbers) are assigned to the list. You can specify it and retrieve it

① Starts as 0 from the left end and can be retrieved by specifying a numerical value.
② The last one-As 1
-2,-You can also take out in order of 3.
List = [0, 1, 2, 3]
print(List [0]) 
#"0" with index 0 is output

List = [0, 1, 2, 3]
print(List [-3]) 
#The third "1" from the back is output

Extract range value from multiple list.

① Number:Take out from the specified number with
②:Take out to the front of the number with the number(-In the case of, counting from the left)
al = ["0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
print(al[1:4]) 
#Index 1-3: ["1", "2", "3"]

print(al[:4]) 
#Index 0-3: ["0", "1", "2", "3"]

print(al[7:]) 
#Index 7 ~ End["7", "8", "9","10"]Is output

print(al[0:100]) 
#All output because it exceeds the range

print(al[0:-5]) 
#Index 0 to 6th from the back["0","1", "2", "3", "4","5"]

Overwrite and add list

As an additional method
① Number designation
② Finally with append(Only one can be done)
③ +=And finally(Can be multiple)


al = ["0","1", "2", "3", "4"]
al[0] = "A" 
print(al) 
#Output result: ["A", "1", "2", "3", "4"]


al = ["0","1", "2", "3", "4"]
al[1:3] = ["B", "C"] 
print(al) 
#Output result: ["0", "B", "C", "3", "4"]

al = ["0","1", "2", "3", "4"]
al.append("f") 
print(al) 
#Output result: ["0","1", "2", "3", "4","f"]

al = ["0","1", "2", "3", "4"]
al += ["f","g"] 
print(al)
#Output result: ["0","1", "2", "3", "4","f","g"]

Delete element

del list[index]
al = ["0","1", "2", "3", "4"]
del al[3:] 
print(al) 
#Output result: ["0", "1", "2"]


al = ["0","1", "2", "3", "4"]
del al[1:3] 
print(al) # ["0", "3", "4"]Is output

copy

When just assigning a list variable to a list variable
When the assignment destination is changed, the assignment source also changes.
#When only the contents of the list
list() #Use the one on the left.


#Variable as it is
al = ['0', '1', '2']
al_copy = al 
al_copy[0] = 'A' 
print(al_copy)
print(al)

#Output result

['A', '1', '2']
['A', '1', '2']


# list()Only the contents using
al = ['0', '1', '2']
al_copy = list(al)
al_copy[0] = 'A'
print(al_copy) 
print(al) 

#Output result

['A', '1', '2']
['0', '1', '2']

Recommended Posts

Python basics: list
Python basics ⑤
Python basics
Python basics ④
Python basics ③
Python basics
[Python] list
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Paiza Python Primer 4: List Basics
Python basics memorandum
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Basics of Python ①
Basics of python ①
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python> Comprehension / Comprehension> List comprehension
Python array basics
Python profiling basics
Python #Numpy basics
Python basics: functions
About the basics list of Python basics
Python list manipulation
#Python basics (class)
Python basics summary
Sorted list in Python
Python Exercise 2 --List Comprehension
List of python modules
Python> list> extend () or + =
Python basics ② for statement
Python: Unsupervised Learning: Basics
Basics of Python scraping basics
Python list comprehension speed
Python basics 8 numpy test
Filter List in Python
python unittest assertXXX list
Errbot: Python chatbot basics
#Python DeepLearning Basics (Mathematics 1/4)
Python3 List / dictionary memo
[Memo] Python3 list sort
OpenCV3 Python API list
Python error list (Japanese)
Python basics: Socket, Dnspython
List find in Python
# 4 [python] Basics of functions
Basics of python: Output
Python exception class list
Initialize list with python
Python hand play (two-dimensional list)
Python list, for statement, dictionary
Summary of Python3 list operations
[Python] Convert list to Pandas [Pandas]
Python