Summary of Python3 list operations

From http://www.amazon.co.jp/dp/4873117380

slice

First of all, the idea of slicing

-- [:]` `` extracts the entire sequence from beginning to end -- [start:] `` extracts the sequence from the start offset to the end --``` [: end] extracts the sequence from the beginning to the end-1 offset --``` [start: end] `` extracts the sequence from start offset to end-1 offset --``` [start: end: step] `` `extracts the sequence from start offset to end-1 offset for each step character

The offset is 0, 1, ... from zero to the right and -1, -2, ... from the end to the left. If start is not specified, the slice uses 0 (start).

Example

In [9]: sample_list = ['a','b','c','d','e']

In [10]: sample_list[0]
Out[10]: 'a'

In [11]: sample_list[0:]
Out[11]: ['a', 'b', 'c', 'd', 'e']

In [12]: sample_list[0:-1]
Out[12]: ['a', 'b', 'c', 'd']

In [13]: sample_list[0:4]
Out[13]: ['a', 'b', 'c', 'd']

In [19]: sample_list[-3:]
Out[19]: ['c', 'd', 'e']

In [22]: sample_list[-3:-1]
Out[22]: ['c', 'd']

#When flipping the list
In [14]: sample_list[::-1]
Out[14]: ['e', 'd', 'c', 'b', 'a']

append() Use the ʻappend () `method when you want to add an element to the end of the list

In [23]: sample_list.append('f')

In [24]: sample_list
Out[24]: ['a', 'b', 'c', 'd', 'e', 'f']

Combine lists using extend () or + =

You can use extend () to combine the two lists into one. This is a destructive operation.

In [27]: sample_list.extend(number_list)

In [28]: sample_list
Out[28]: ['a', 'b', 'c', 'd', 'e', 'f', 1, 2, 3]

# sample_Initialize list
In [33]: sample_list = ['a','b','c','d','e']

In [34]: sample_list += number_list

In [35]: sample_list
Out[35]: ['a', 'b', 'c', 'd', 'e', 1, 2, 3]

By the way, append () is treated as one element, so

In [36]: sample_list = ['a','b','c','d','e']
In [37]: sample_list.append(number_list)

In [38]: sample_list
Out[38]: ['a', 'b', 'c', 'd', 'e', [1, 2, 3]]

Add element with offset specified by insert ()

The append () function can only append an element at the end of the list. Use insert () when you want to specify the offset of the list and add an element before it. If offset 0 is specified, it will be inserted at the beginning. If you specify an offset beyond the end, it will be inserted at the end of the list, just like append ().

In [42]: sample_list = ['a','b','c','d','e']

In [43]: sample_list.insert(1,1)

In [44]: sample_list
Out[44]: ['a', 1, 'b', 'c', 'd', 'e']

In [45]: sample_list.insert(10,10)

In [46]: sample_list
Out[46]: ['a', 1, 'b', 'c', 'd', 'e', 10]

Delete element of offset specified by del

Note that del is a statement, not a method.

In [46]: sample_list
Out[46]: ['a', 1, 'b', 'c', 'd', 'e', 10]

In [48]: del sample_list[-1]

In [49]: sample_list
Out[49]: ['a', 1, 'b', 'c', 'd', 'e']

(Del, len, and non-unified implementations are pretty confusing ..)

Remove elements based on value with remove ()

If you don't know the offset, or if you don't care where it is, you can use remove () to specify a value to remove the element.

In [49]: sample_list
Out[49]: ['a', 1, 'b', 'c', 'd', 'e']

In [50]: sample_list.remove(1)

In [51]: sample_list
Out[51]: ['a', 'b', 'c', 'd', 'e']

How to retrieve and delete an element by specifying an offset with pop ()

You can use pop () to pop an element out of the list and remove it from the list at the same time. Calling pop () with an offset returns the element for that offset. If no argument is given, -1 is used as the offset.

In [51]: sample_list
Out[51]: ['a', 'b', 'c', 'd', 'e']

In [52]: sample_list.pop()
Out[52]: 'e'

In [53]: sample_list
Out[53]: ['a', 'b', 'c', 'd']

In [54]: sample_list.pop(1)
Out[54]: 'b'

In [55]: sample_list
Out[55]: ['a', 'c', 'd']

How to know the element offset from the element value by index ()

In [56]: sample_list = ['a','b','c','d','e']

In [57]: sample_list.append('a')

In [58]: sample_list
Out[58]: ['a', 'b', 'c', 'd', 'e', 'a']

In [60]: sample_list.index('b')
Out[60]: 1

#If there are multiple, the one with the smaller offset
In [61]: sample_list.index('a')
Out[61]: 0

Test for the presence or absence of values using in

Use in to test if the list has a value.

In [64]: sample_list
Out[64]: ['a', 'b', 'c', 'd', 'e']

In [65]: 'a' in sample_list
Out[65]: True

In [66]: 'g' in sample_list
Out[66]: False

Calculate the number of values using count ()

Use count () to count how many specific values are in the list.

In [64]: sample_list
Out[64]: ['a', 'b', 'c', 'd', 'e']

In [67]: sample_list.count('a')
Out[67]: 1

In [68]: sample_list.count('g')
Out[68]: 0

In [69]: sample_list.append('a')

In [70]: sample_list
Out[70]: ['a', 'b', 'c', 'd', 'e', 'a']

In [71]: sample_list.count('a')
Out[71]: 2

Sorting elements by sort ()

There are two sorts --Method sort () is a destructive operation --The general-purpose function sorted () is a non-destructive operation. --Returns a copy of the sorted list

In [76]: names = [ 'Ryoma', 'Toshimichi', 'Tomoatsu']

In [77]: sorted_named = sorted(names)

In [78]: sorted_named
Out[78]: ['Ryoma', 'Tomoatsu', 'Toshimichi']

In [79]: names.sort()

In [80]: names
Out[80]: ['Ryoma', 'Tomoatsu', 'Toshimichi']

Integers and floats can be used together because Python automatically converts them in the expression.

In [81]: numbers = [2, 1, 4.0, 3]

In [82]: numbers.sort()

In [83]: numbers
Out[83]: [1, 2, 3, 4.0]

By default, it is in ascending order,```reverse=True``It can also be done in descending order.

In [84]: numbers = [2, 1, 4.0, 3]

In [85]: numbers.sort(reverse=True)

In [86]: numbers
Out[86]: [4.0, 3, 2, 1]

=Substitution and copy by()Copy by

=Then, note that the reference destination is the same.

In [87]: a = [1, 2, 3]

In [88]: b = a

In [89]: b[0] = 'why'

In [90]: b
Out[90]: ['why', 2, 3]

In [91]: a
Out[91]: ['why', 2, 3]

Use one of the following to copy -List copy()function

In [92]: a = [1, 2, 3]

In [93]: b = a.copy()

In [94]: c = list(a)

In [95]: d = a[:]

In [96]: a
Out[96]: [1, 2, 3]

In [97]: b
Out[97]: [1, 2, 3]

In [98]: c
Out[98]: [1, 2, 3]

In [99]: d
Out[99]: [1, 2, 3]

In [100]: a[0] = 'why'

In [101]: a
Out[101]: ['why', 2, 3]

In [102]: b
Out[102]: [1, 2, 3]

In [103]: c
Out[103]: [1, 2, 3]

In [104]: d
Out[104]: [1, 2, 3]

list()About conversion function

list()The conversion function generates a list when used alone, but when taking a list or tuple as an argument, it expands the outermost part of the contents and lists it.

In [107]: a = [1, 2, 3]

In [108]: b = (1, 2, 3)

In [109]: c = list(a)

In [110]: c
Out[110]: [1, 2, 3]

In [111]: d = list(b)

In [112]: d
Out[112]: [1, 2, 3]

In [113]: a.append(b)

In [114]: a
Out[114]: [1, 2, 3, (1, 2, 3)]

In [115]: c = list(a)

In [116]: c
Out[116]: [1, 2, 3, (1, 2, 3)]

Recommended Posts

Summary of Python3 list operations
Summary of python file operations
List of python modules
Summary of string operations
Summary of built-in methods in Python list
Summary of Python arguments
Summary of how to use Python list
[Python / DynamoDB / boto3] List of operations I tried
[Python] Summary of S3 file operations with boto3
Summary of Excel operations using OpenPyXL in Python
[Python] Copy of multidimensional list
Python Summary
Summary of Python sort (list, dictionary type, Series, DataFrame)
Python summary
[Python] list
A brief summary of Python collections
Summary of various operations in Tensorflow
About the basics list of Python basics
Summary of Python indexes and slices
[OpenCV; Python] Summary of findcontours function
[Python] Summary of how to use pandas
Python basics: list
Introduction of Python
Python tutorial summary
Display a list of alphabets in Python 3
Summary of various for statements in Python
[Python] Summary of array generation (initialization) time! !! !!
Basics of Python ①
Basics of python ①
[Python2.7] Summary of how to use unittest
Copy of python
[python] Get a list of instance variables
Basic grammar of Python3 series (list, tuple)
Python> Comprehension / Comprehension> List comprehension
Summary of useful techniques for Python Scrapy
python related summary
[Python2.7] Summary of how to use subprocess
Axis option specification summary of Python "numpy.sum (...)"
[Python] Get a list of folders only
Python list manipulation
Python basics summary
[Python] Understand list slicing operations in seconds
Introduction of Python
Summary of operations often performed with asyncpg
[Python3] Understand the basics of file operations
[python] Check the elements of the list all, any
Correspondence summary of array operation of ruby and python
[Python] Sort the list of pathlib.Path in natural sort
Summary of the differences between PHP and Python
Summary of how to import files in Python 3
[python] Create a list of various character types
[Introduction to Udemy Python 3 + Application] 19. Copy of list
Basic operation list of Python3 list, tuple, dictionary, set
Make a copy of the list in Python
Summary of how to use MNIST in Python
Installation of Python3 and Flask [Environment construction summary]
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
Summary of frequently used Python arrays (for myself)
Summary of studying Python to use AWS Lambda
I / O related summary of python and fortran
List of Python code to move and remember