Summary of how to use Python list

Introduction

This is a summary for those who have studied once, but "What was this? How was this ...".

Python can now be selected in FE (Fundamental Information Technology Engineer Examination). Originally, this spring exam was scheduled to be the first question, but it was canceled due to the influence of Corona.

Such a Python list is also given in the FE example, so I tried to summarize how to use it.

You can look back just before the exam, or remember the methods that you tend to forget in everyday programming. Please use all means.

Initialize list

First of all, from the basic list declaration.

alist = []
blist = [1, 1, 2, 3, 5, 8, 13]
clist = list(range(5))
dlist = [i for i in range(10) if (i % 2 == 0)]  #List comprehension

print(f"alist = {alist}\nblist = {blist}\nclist = {clist}\ndlist = {dlist}")

output


alist = []
blist = [1, 1, 2, 3, 5, 8, 13]
clist = [0, 1, 2, 3, 4]
dlist = [0, 2, 4, 6, 8]

Substitution and copy

It's a blind spot. Therefore, in the following handling of list, both passing by reference and passing by value will be explained.

--Substitute list with = --Pass by reference --The assignment source (ʻalist) changes → blistalso changes --Substitute the return value of the.copy () method --Pass by value --The assignment source (ʻalist) has changed → clist has not changed

alist = [1, 2, 3]

#Pass by reference
blist = alist

#Pass by value
clist = alist.copy()

def delete_middle(list_obj, msg):
    list_obj.pop(int(len(list_obj)/2))
    print(msg, list_obj)
    
delete_middle(alist, "alist =")
print(f"blist = {blist}")
print(f"clist = {clist}")

output


alist = [1, 3]
blist = [1, 3]
clist = [1, 2, 3]

Add element to list

--Add only one element to the end --. Append (x) method --Add one to the specified index location --. Insert (index, obj) method --Add list to list --Use + operator --Even if the elements of blist and clist change after the operation, it does not affect dlist.

alist = [1, 2, 3]
blist = alist
clist = alist.copy()

#Add 4 to the back
alist.append(4)

#Add 100 to index of 1
alist.insert(1, 100)

#Add list to list
dlist = blist + clist

print(f"alist = {alist}\nblist = {blist}\nclist = {clist}\ndlist = {dlist}")
alist = [1, 100, 2, 3, 4]
blist = [1, 100, 2, 3, 4]
clist = [1, 2, 3]
dlist = [1, 100, 2, 3, 4, 1, 2, 3]

Delete the contents of list

Delete by specifying index

alist = [1, 3, 5, 7, 9]
blist = alist
clist = alist.copy()

del alist[2]
print(f"del alist[2] =>\n\talist = {alist}\n\tblist = {blist}\n\tclist = {clist}\n")

alist.pop(2)
print(f"alist.pop(2) =>\n\talist = {alist}\n\tblist = {blist}\n\tclist = {clist}")

output


del alist[2] =>
	alist = [1, 3, 7, 9]
	blist = [1, 3, 7, 9]
	clist = [1, 3, 5, 7, 9]

alist.pop(2) =>
	alist = [1, 3, 9]
	blist = [1, 3, 9]
	clist = [1, 3, 5, 7, 9]

Delete by specifying a value

Only the value at the beginning is deleted. Not all of its values.

alist = [1, 2, 2, 3, 3, 3]
blist = alist
clist = alist.copy()

alist.remove(2)

print(f"alist = {alist}\nblist = {blist}\nclist = {clist}")

output


alist = [1, 2, 3, 3, 3]
blist = [1, 2, 3, 3, 3]
clist = [1, 2, 2, 3, 3, 3]

Delete the last item

If you do not put an argument in .pop (), it is the same as .pop (-1), and the last element of the list is deleted.

alist = [1, 3, 5, 7, 9]
blist = alist
clist = alist.copy()

alist.pop()
print(f"alist.pop(2) =>\n\talist = {alist}\n\tblist = {blist}\n\tclist = {clist}")

output


alist.pop() =>
	alist = [1, 3, 5, 7]
	blist = [1, 3, 5, 7]
	clist = [1, 3, 5, 7, 9]

Delete all

alist = [1, 2, 3]
blist = alist
clist = alist.copy()

# alist = []The same thing is done.
# alist = []It's confusing with the declaration, but it's the development team's preference.
alist.clear()

print(f"alist = {alist}\nblist = {blist}\nclist = {clist}")

output


alist = []
blist = []
clist = [1, 2, 3]

Sort by value

alist = [20, 1, 5, 13, 8]
blist = alist
clist = alist.copy()

alist.sort()

print(f"alist = {alist}\nblist = {blist}\nclist = {clist}")

output


alist = [1, 5, 8, 13, 20]
blist = [1, 5, 8, 13, 20]
clist = [20, 1, 5, 13, 8]

Examine the index of the first value to appear

alist = [333, 1, 333, 22, 333, 22]
#22 is in what order
index = alist.index(22)
print(f"index = {index}")

output


index = 3

How many values are included

alist = [0, 1, 1, 0, 1, 0 ,1, 0, 0]
#How many times 1 came out
count = alist.count(1)
print(f"count = {count}")

output


count = 4

at the end

I think that the list method of the major is suppressed. I wanted to explain the behavior of passing by reference and passing by value, so it became difficult to read. Excuse me mm

I hope you can use it as a memorandum. Well then.

Recommended Posts

Summary of how to use Python list
[Python] Summary of how to use pandas
[Python2.7] Summary of how to use unittest
[Python2.7] Summary of how to use subprocess
[Python] How to use list 1
Summary of how to use MNIST in Python
Summary of how to use pandas.DataFrame.loc
Summary of how to use pyenv-virtualenv
[Python] How to use list 3 Added
Summary of how to use csvkit
How to use list []
[Question] How to use plot_surface of python
[Python] Summary of how to use split and join functions
[Python] How to use two types of type ()
python3: How to use bottle (2)
Summary of how to import files in Python 3
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Algorithm x Python] How to use the list
Summary of studying Python to use AWS Lambda
[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
I tried to summarize how to use matplotlib of python
How to write a list / dictionary type of Python3
[Python] Summary of how to specify the color of the figure
Python: How to use async with
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
How to use Mysql in python
How to use OpenPose's Python API
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 use python zip function
[Python] How to use Typetalk API
[Python] How to make a list of character strings character by character
[python] Summary of how to retrieve lists and dictionary elements
How to shuffle a part of a Python list (at random.shuffle)
Comparison of how to use higher-order functions in Python 2 and 3
How to get a list of built-in exceptions in python
[Introduction to Python] How to use class in Python?
scikit-learn How to use summary (machine learning)
How to install and use pandas_datareader [Python]
[python] How to use __command__, function explanation
How to calculate Use% of df command
[Python] How to use import sys sys.argv
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
python: How to use locals () and globals ()
How to use __slots__ in Python class
Summary of built-in methods in Python list
How to use "deque" for Python data
Basics of PyTorch (1) -How to use Tensor-
How to use Python zip and enumerate