[Python] Manipulation of elements in list (array) [Add / Delete]

Introduction

We have summarized the ** add / remove ** methods that are important for working with lists. Basically, write the code and output result with the following code.

ex.py


code = 'code'
print(code)
# code(Output result)

Add elements to a list (array) append, extend, insert

To add an element to a list (array) of type list or combine another list in Python, use the list methods ʻappend (), ʻextend (), ʻinsert ()`. In addition, there is also a method of assigning by specifying the position with the + operator or slice.

-Append element at the end: append () -Combine another list or tuple at the end (concatenate): extend (), + operator -Add (insert) element at specified position: insert () -Add another list or tuple to the specified position (insert): Use slice

-Append (): An element can be added at the end (last).

append().py


l = [0, 1, 2]
print(l)
# [0, 1, 2]

l.append(100)
print(l)
# [0, 1, 2, 100]

l.append('new')
print(l)
# [0, 1, 2, 100, 'new']

l.append([3, 4, 5])
print(l)
# [0, 1, 2, 100, 'new', [3, 4, 5]]

・ Extend () ・ ・ ・ You can combine another list or tuple at the end (last).

extend().py


l = [0, 1, 2]

l.extend([100, 101, 102])
print(l)
# [0, 1, 2, 100, 101, 102]

l.extend((-1, -2, -3))
print(l)
# [0, 1, 2, 100, 101, 102, -1, -2, -3]

#append()Unlike, it is stored character by character
l.extend('new')
print(l)
# [0, 1, 2, 100, 101, 102, -1, -2, -3, 'n', 'e', 'w']

#extend()Not a method+=But yes.
l += [5, 6, 7]
print(l)
# [0, 1, 2, 100, 101, 102, -1, -2, -3, 'n', 'e', 'w', 5, 6, 7]

-Insert (): An element can be added (inserted) at a specified position. Specify the position in the first argument and the element to be inserted in the second argument. The beginning (first) is 0. If the value is negative, -1 is one before the end (last).

insert().py


l = [0, 1, 2]

l.insert(0, 100)
print(l)
# [100, 0, 1, 2]

l.insert(-1, 200)
print(l)
# [100, 0, 1, 200, 2]

l.insert(0, [-1, -2, -3])
print(l)
# [[-1, -2, -3], 100, 0, 1, 200, 2]

-If you specify a range in a slice and substitute another list or tuple, all elements will be added (inserted).

insert().py


l = [0, 1, 2]

l[1:1] = [100, 200, 300]
print(l)
# [0, 100, 200, 300, 1, 2]

l[1:2] = [100, 200, 300]
print(l)
# [0, 100, 200, 300, 2]

Clear, pop, remove, del to remove elements in a list (array)

To remove a list (array) element of type list in Python, use the list methods claer (), pop (), remove (). In addition, there is also a method of specifying the position / range with an index or slice and deleting it with the del statement.

-Delete all elements: clear () -Delete the element at the specified position and get the value: pop () -Search for the same element as the specified value and remove the first element: remove () -Delete by specifying the position / range in the index slice: del

・ Clear () ・ ・ ・ Delete all elements

clear().py


l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

l.clear()
print(l)
# []

-Pop (): You can delete the element at the specified position with the list method pop () and get the value of that element.

pop().py


l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(l.pop(3))
# 4

print(l.pop(-2))
# 8

#If you omit the argument and do not specify the position, delete the last element
print(l.pop())
# 9

-Remove (): The list method remove () can search for the same element as the specified value and remove the first element.

remove().py


l = ['Alice', 'Bob', 'Charlie', 'Bob', 'Dave']

l.remove('Alice')
print(l)
# ['Bob', 'Charlie', 'Bob', 'Dave']

・ Del ・ ・ ・ Specify the element you want to delete by index. The beginning (first) is 0 and the end (last) is -1.

del.py


l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

del l[6]
print(l)
# [1, 2, 3, 4, 5, 6, 8]

del l[-1]
print(l)
# [1, 2, 3, 4, 5, 6, 7, 8]

#You can delete multiple elements at once by specifying a range with a slice.
del l[2:5]
print(l)
# [0, 1, 5, 6, 7, 8, 9]

Finally

I would be grateful if you could tell me if there is any other solution.

Recommended Posts

[Python] Manipulation of elements in list (array) [Add / Delete]
Delete multiple elements in python list
Randomly select elements from list (array) in python
[Python] Manipulating elements in a list (array) [Sort]
Getting list elements in Python
[Python] Outputs all combinations of elements in the list
Group by consecutive elements of a list in Python
Pixel manipulation of images in Python
Get the number of specific elements in a python list
Display a list of alphabets in Python 3
Summary of built-in methods in Python list
Python list manipulation
[Python] Combine all the elements in the array
Extract every n elements from an array (list) in Python and Ruby
[python] Check the elements of the list all, any
[Python] Sort the list of pathlib.Path in natural sort
Make a copy of the list in Python
Rewriting elements in a loop of lists (Python)
Output in the form of a python array
Speed: Add element to end of Python array
Sort list elements in a specified order in Python
How to remove duplicate elements in Python3 list
Sorted list in Python
String manipulation in python
Date manipulation in Python
Filter List in Python
List find in Python
Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple
[python] Get the list of classes defined in the module
Get the size (number of elements) of UnionFind in Python
List of Python code used in big data analysis
How to check in Python if one of the elements of a list is in another list
Summary of Python3 list operations
Empty multidimensional array in python
List of nodes in diagrams
Equivalence of objects in Python
Multidimensional array initialization of list
[Python] Copy of multidimensional list
String date manipulation in Python
Implementation of quicksort in Python
Make sure all the elements in the list are the same in Python
Basic summary of data manipulation in Python Pandas-Second half: Data aggregation
How to swap elements in an array in Python, and how to reverse an array.
(For myself) Flask_8 (Add / Edit / Delete in database with python)
Decrypt one line of code in Python lambda, map, list
Get index of nth largest / smallest value in list in Python
How to get a list of built-in exceptions in python
Get index of nth largest / smallest value in list in Python
[Python] How to delete rows and columns in a table (list of drop method options)
python web scraping-get elements in bulk
File / folder path manipulation in Python
Difference between list () and [] in Python
[python] Manage functions in a list
Output 2017 Premium Friday list in Python
Python> list> append () and extend ()> append: list is added | extend: list elements are added | + = to add list
Try to get a list of breaking news threads in Python.
[Python] Display only the elements of the list side by side [Vertical, horizontal]
[Python] Let's reduce the number of elements in the result in set operations
Division of timedelta in Python 2.7 series
MySQL-automatic escape of parameters in python
Handling of JSON files in Python