[Introduction to Udemy Python3 + Application] 17. List operation

** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **

■ List operation

◆ Replace list elements
>>> s = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> s[0] = 'A'
>>> s
['A', 'b', 'c', 'd', 'e', 'f', 'g']
>>> s[2:5] = ['C', 'D', 'E']
>>> s
['A', 'b', 'C', 'D', 'E', 'f', 'g']

You can directly assign a character string by specifying an index or slice in the list.

.append () and .insert ()
>>> n = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> n
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> n.append(100)
>>> n
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100]
>>> n.insert(0, 200)
>>> n
[200, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100]

.append adds a string to the end of the list. You can use .insert to specify an arbitrary index and insert a string at that location.

.pop()

>>> n = [200, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 300]
>>> n
[200, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 300]
>>> n.pop(0)
200
>>> n
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 300]
>>> n.pop()
300
>>> n
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

If you specify an index with .pop (), that element will be extracted. The extracted elements disappear from the list. If no index is specified, the last element will be extracted.

◆ Erase list elements (replace with empty list)
>>> s = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> s[2:5] = []
>>> s
['a', 'b', 'f', 'g']

You can delete an element by replacing the specified part with an empty list.

◆ Erase list elements (use del)
>>> n = [1, 2, 3, 4, 5]
>>> n
[1, 2, 3, 4, 5]
>>> del n[0]
>>> n
[2, 3, 4, 5]
>>> del n
>>> n
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined

You can delete the indexed one with del. Note that if you inadvertently multiply n by del without specifying an index, n itself will be deleted.

◆ Remove the elements of the list (use .remove ())
>>> n = [1, 2, 3, 4, 5]
>>> n
[1, 2, 3, 4, 5]
>>> n.remove(2)
>>> n
[1, 3, 4, 5]
>>> n.remove(4)
>>> n
[1, 3, 5]
>>> n.remove(6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list

You can use .remove () to directly specify and remove an element. At this time, if an element that does not exist in the list is specified, an error will occur.

◆ Add another list element to the list
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> x = a + b
>>> x
[1, 2, 3, 4, 5, 6]

This is as I did before.

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a += b
>>> a
[1, 2, 3, 4, 5, 6]

+ = Adds the elements in the list of b to the list of ʻa`.

>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> x.extend(y)
>>> x
[1, 2, 3, 4, 5, 6]

You can also use a method called .extend ().

Recommended Posts

[Introduction to Udemy Python3 + Application] 17. List operation
[Introduction to Udemy Python3 + Application] 18. List methods
[Introduction to Udemy Python3 + Application] 16. List type
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
[Introduction to Udemy Python 3 + Application] 19. Copy of list
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 57. Decorator
[Introduction to Udemy Python 3 + Application] 56. Closure
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Udemy Python 3 + Application] Summary
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
[Introduction to Udemy Python3 + Application] 28. Collective type
[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
[Introduction to Udemy Python3 + Application] 33. if statement
[Introduction to Udemy Python3 + Application] 13. Character method
[Introduction to Udemy Python3 + Application] 55. In-function functions
[Introduction to Udemy Python3 + Application] 48. Function definition
[Introduction to Udemy Python 3 + Application] 10. Numerical values
[Introduction to Udemy Python3 + Application] 21. Tuple type
[Introduction to Udemy Python3 + Application] 45. enumerate function
[Introduction to Udemy Python3 + Application] 65. Exception handling
[Introduction to Udemy Python3 + Application] 11. Character strings
[Introduction to Udemy Python3 + Application] 44. range function
[Introduction to Udemy Python3 + Application] 46. Zip function
[Introduction to Udemy Python3 + Application] 24. Dictionary type
[Introduction to Udemy Python3 + Application] 8. Variable declaration
[Introduction to Udemy Python3 + Application] 29. Set method
[Introduction to Udemy Python3 + Application] 61. Dictionary comprehension
[Introduction to Udemy Python 3 + Application] 22. Tuple unpacking
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
[Introduction to Udemy Python3 + Application] 23. How to use tuples
[Introduction to Udemy Python 3 + Application] 38. When judging None
[Introduction to Udemy Python3 + Application] 40.while else statement
[Introduction to Udemy Python3 + Application] 62. Set comprehension notation
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
[Introduction to Udemy Python3 + Application] 43. for else statement
[Introduction to Udemy Python3 + Application] 67. Command line arguments
[Introduction to Python] <list> [edit: 2020/02/22]
[Introduction to Udemy Python3 + Application] 9. First, print with print
[Introduction to Udemy Python 3 + Application] 54. What is Docstrings?
[Introduction to Udemy Python3 + Application] 14. Character substitution 15.f-strings
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
[Introduction to Udemy Python 3 + Application] 66. Creating your own exceptions
[Introduction to Udemy Python3 + Application] 53. Dictionary of keyword arguments
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Introduction to Udemy Python3 + Application] 68. Import statement and AS
[Introduction to Udemy Python3 + Application] 52. Tupleization of positional arguments
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
[Introduction to Udemy Python3 + Application] 39. while statement, continue statement and break statement
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Introduction to Udemy Python3 + Application] 32.1 When one line becomes long
[Introduction to Udemy Python3 + Application] 50. Positional arguments, keyword arguments, and default arguments
[Introduction to Udemy Python3 + Application] 51. Be careful with default arguments
[Introduction to Udemy Python3 + Application] 69. Import of absolute path and relative path
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
Introduction to Python language
Introduction to OpenCV (python)-(2)
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
Introduction to Python Django (2) Win
[Python] Convert list to Pandas [Pandas]