Python list manipulation

List join => extend ()

How to write Contents
s.extend(t) Add the contents of t to the object s
s += others s.extend(others)Will have the same result as

Add element at specified position => insert ()

How to write Contents
s.insert(i,x) I of object s(index)Insert x at the position specified by

Delete the element specified by the index => del

How to write Contents
del s[i:j] Delete elements from indexes i to j

Remove an element whose position you do not know => remove ()

How to write Contents
s.remove(x) Remove the first element that matches x from s

Know the index of an element => index ()

How to write Contents
s.index(x) Returns the index where x first appears in s

Is there a value => in

How to write Contents
x in s Returns True if the element of object s has x, False otherwise

How many values are in the list => count ()

How to write Contents
s.count(x) Returns the number of times x appears in s

Sorting elements => sort ()

List (list) Sort elements with a method dedicated to objects

How to write Contents
s.sort() Sort the elements of s in ascending order
s.sort(reverse=True) Sort the elements of s in descending order

Copy of list

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

result

[1,2,3]
['Hogehoge',2,3]
['Hogehoge',2,3]

The operation for list a is also reflected in list b → "Substitution of reference"

To make a "real copy of the list"

a = [1,2,3]
b = a.copy()
c = list(a)
d = a[:]
a[0] = 'Hogehoge'
print(a)
print(b)
print(c)
print(d)

result

['Hogehoge',2,3]
[1,2,3]
[1,2,3]
[1,2,3]

Recommended Posts

Python list manipulation
[Python] list
Python basics: list
Python Grammar-String Manipulation
[Python] Chapter 04-02 Various data structures (list manipulation)
List of python modules
Python> list> extend () or + =
Python Node.js character manipulation
String manipulation in python
Python list comprehension speed
Filter List in Python
python unittest assertXXX list
Python string manipulation master
[Memo] Python3 list sort
OpenCV3 Python API list
Python error list (Japanese)
List find in Python
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
[Python] How to use list 1
Create ToDo List [Python Django]
[Python] Manipulation of elements in list (array) [Add / Delete]
Specify multiple list indexes (Python)
Python Basic Course (5 List Tuples)
Python list is not a list
[Introduction to Python] <list> [edit: 2020/02/22]
Python list and tuples and commas
Regular expression manipulation with Python
Paiza Python Primer 4: List Basics
Python list comprehensions and generators
[Python / PyQ] 4. list, for statement
String date manipulation in Python
Getting list elements in Python
Extract multiple list duplicates in Python
Pixel manipulation of images in Python
Difference between list () and [] in Python
[python] Manage functions in a list
Output 2017 Premium Friday list in Python
Convert list to DataFrame with python
[Python beginner] Divide one list (5 lines).
python / Make a dict from a list.
[Python] How to use list 3 Added
Write python list fast vim tips
Delete multiple elements in python list
kafka python
[Introduction to Udemy Python3 + Application] 18. List methods
Python basic operation 1st: List comprehension notation
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service