[Introduction to Python3 Day 6] Chapter 3 Py tool lists, tuples, dictionaries, sets (3.2.7-3.2.19)

3.2.7 Adding an element to the end with append ()

Traditionally, append () is used to add elements to the list one by one.

>>> marxes=["TTTTTT","aaaa","bbbb"]
>>> marxes.append("Zeppo")
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', 'Zeppo']

3.2.8 Joining lists using extend () or + =

You can combine two ** lists ** with extend ().


>>> marxes
['TTTTTT', 'aaaa', 'CCCC', 'Zeppo']
>>> others=["Gummo","Karl"]
>>> marxes.extend(others)
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', 'Zeppo', 'Gummo', 'Karl']
>>> marxes=["TTTTTT",'aaaa', 'CCCC']
>>> others=["Gummo","Karl"]
>>> marxes+=others
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', 'Gummo', 'Karl']

At this time, if append () is used, it will be added as one list element instead of other elements. This also shows that the list can have different types.

>>> marxes=["TTTTTT",'aaaa', 'CCCC']
>>> others=["Gummo","Karl"]
>>> marxes.append(others)
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', ['Gummo', 'Karl']]
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', ['Gummo', 'Karl']]

3.2.9 Add element with offset specified by insert ()

The append () function could only add an element to the end of the list, but if you want to specify an offset for the list and add an element before it, use the insert () function.


>>> marxes.insert(3,"Gummo")
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', 'Gummo', ['Gummo', 'Karl']]

#If you specify an offset that exceeds the end, append()It is inserted at the end of the list in the same way as.
>>> marxes.insert(10,"XXXXXX")
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', 'Gummo', ['Gummo', 'Karl'], 'XXXXXX']

3.2.10 Deletion of elements with specified offset by del

>>> marxes=["Groucho","Choico", 'Haapo',"Gummo","Zepppo"]
>>> del marxes[-1]
>>> marxes
['Groucho', 'Choico', 'Haapo', 'Gummo']
>>> marxes=["Groucho","Choico", 'Haapo',"Gummo","Zepppo"]
>>> marxes[2]
'Haapo'
>>> del marxes[2]
>>> marxes
['Groucho', 'Choico', 'Gummo', 'Zepppo']

Since del is a Python statement, not a list method, we don't write marxes [-2] .del (). del is the opposite of assignment, separating the name from the Python object and freeing the object's memory if the name is the last reference to the object.

3.2.11 Remove elements based on value with remove ()

If you don't care about the position of the element you want to remove, use remove () to specify a value and remove the element.


>>> marxes=["Groucho","Choico", 'Haapo',"Gummo","Zepppo"]
>>> marxes.remove("Gummo")
>>> marxes
['Groucho', 'Choico', 'Haapo', 'Zepppo']

3.2.12 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 specified, -1 is used as the offset. pop (0) returns the list head, pop () or pop (-1) returns the end.

>>> marxes=["Groucho","Choico", 'Haapo',"Zepppo"]
>>> marxes.pop()
'Zepppo'
>>> marxes
['Groucho', 'Choico', 'Haapo']
>>> marxes.pop(1)
'Choico'
>>> marxes
['Groucho', 'Haapo']

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

If you want to know the offset in the list of elements from the value of the element, use index ().

>>> marxes=["Groucho","Choico", 'Haapo',"Zepppo"]
>>> marxes.index("Choico")
1

3.2.14 Testing for the presence or absence of values using in

Use ** in ** to test if the list has a value, like Python.

>>> marxes=["Groucho","Choico", 'Haapo',"Zepppo"]
>>> "Groucho" in marxes 
True
>>> "Boss" in marxes 
False

3.2.15 Calculation of the number of values using count ()

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


>>> marxes=["Groucho","Choico", 'Haapo',"Zepppo","Zepppo"]
>>> "Zepppo" in marxes 
True
>>> marxes=["Groucho","Choico", 'Haapo',"Zepppo","Zepppo"]
>>> marxes.count("Groucho")
1
>>> marxes.count("Boss")
0
>>> marxes.count("Zepppo")
2

3.2.16 Conversion to string by join ()

Just remember that "join () is the opposite of split ()"


>>> marxes=["Groucho","Choico", 'Haapo']
>>> ",".join(marxes)
'Groucho,Choico,Haapo'
>>> friends=["Harry", "Hermione", "Ron"]
>>> separator=" * "
>>> joined = separator.join(friends)
>>> joined
'Harry * Hermione * Ron'
>>> separated = joined.split(separator)
>>> separated
['Harry', 'Hermione', 'Ron']
>>> separated == joined
True

3.2.17 Sorting elements by sort ()

--The list function sort () sorts the list itself ** on the fly **. --The general function sorted () returns a copy of the sorted list.

If the elements of the list are numbers, they are sorted in ascending order by default. If the element is a string, it will be sorted alphabetically.

>>> marxes=["Groucho","Choico", 'Haapo']

#sorted_The marxes are copies, and making them does not change the original list.
>>> sorted_marxes = sorted(marxes)
>>> sorted_marxes
['Choico', 'Groucho', 'Haapo']
>>> marxes
['Groucho', 'Choico', 'Haapo']

#list function sort from marxes list()Calls to change the marxes list itself.
>>> marxes.sort()
>>> marxes
['Choico', 'Groucho', 'Haapo']
>>> numbers = [2,4.5,1,3]
>>> numbers.sort()
>>> numbers
[1, 2, 3, 4.5]
>>> numbers = [2,4.5,1,3]

#The default is ascending, but reverse=If you add the True argument, it will be in descending order.
>>> numbers.sort(reverse=True)
>>> numbers
[4.5, 3, 2, 1]
>>> numbers = [2,4.5,1,3]

3.2.18 Obtaining the length by len ()

len () returns the number of elements in the list.

>>> marxes=["Groucho","Choico", 'Haapo']
>>> len(marxes)
3

3.2.19 = Substitution and copy ()

If you assign one list to multiple variables, when you rewrite the list with one of them, the other list will be rewritten.


>>> a=[1,2,3]
>>> a
[1, 2, 3]
>>> b = a 
>>> b
[1, 2, 3]
>>> a[0]="surprise"
>>> b
['surprise', 2, 3]
>>> a
['surprise', 2, 3]
>>> b[0]="I hate surprises"
>>> b
['I hate surprises', 2, 3]
>>> a
['I hate surprises', 2, 3]

b refers to the same list object as a. Regardless of which name is used for a or b to rewrite the contents of the list, the operation is reflected in both.

You can copy the list to a new independent list using one of the following methods: --List copy () function --list () conversion function --List slice

>>> a=[1,2,3]

#b,c,d is a copy of a
>>> b=a.copy()
>>> c=list(a)
>>> d=a[:]
>>> a[0] = "integer lists are boring"
>>> a
['integer lists are boring', 2, 3]
>>> b
[1, 2, 3]
>>> c
[1, 2, 3]
>>> d
[1, 2, 3]


Impressions

Withdrawal for one day is difficult. I wonder if I'll go out tomorrow even at noon.

References

"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"

Recommended Posts

[Introduction to Python3 Day 6] Chapter 3 Py tool lists, tuples, dictionaries, sets (3.2.7-3.2.19)
[Introduction to Python3 Day 7] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.3-3.8)
[Introduction to Python3 Day 8] Chapter 4 Py Skin: Code Structure (4.1-4.13)
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Introduction to Python3 Day 2] Chapter 2 Py Components: Numbers, Strings, Variables (2.1)
[Introduction to Python3 Day 4] Chapter 2 Py Components: Numbers, Strings, Variables (2.3.7-2.4)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
Python3 | Lists, Tuples, Dictionaries
Python lists, tuples, dictionaries
How to use lists, tuples, dictionaries, and sets
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Introduction to Python3 Day 23] Chapter 12 Become a Paisonista (12.1 to 12.6)
[Introduction to Python3 Day 20] Chapter 9 Unraveling the Web (9.1-9.4)
Save lists, dictionaries and tuples to external files python
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python3 Day 10] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.4-5.7)
[Introduction to Python3 Day 9] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.1-5.4)
Introduction to Effectiveness Verification Chapter 1 in Python
[Introduction to Udemy Python3 + Application] 23. How to use tuples
Introduction to effectiveness verification Chapter 3 written in Python
Introduction to Effectiveness Verification Chapter 2 Written in Python
[Chapter 5] Introduction to Python with 100 knocks of language processing
Introduction to Python language
Python> Tuples versus Lists
Introduction to OpenCV (python)-(2)
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
[Chapter 4] Introduction to Python with 100 knocks of language processing
Introduction to Python Django (2) Win
Introduction to serial communication [Python]
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
Python learning memo for machine learning by Chainer Chapter 10 Introduction to Cupy
I read "Reinforcement Learning with Python: From Introduction to Practice" Chapter 1
Python learning memo for machine learning by Chainer Chapter 9 Introduction to scikit-learn
I read "Reinforcement Learning with Python: From Introduction to Practice" Chapter 2
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Introduction to Udemy Python 3 + Application] 57. Decorator
[Introduction to Python] How to parse JSON
[Introduction to Udemy Python 3 + Application] 56. Closure
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
I'm addicted to Python 2D lists
Introduction to image analysis opencv python
[Introduction to Python] Let's use pandas
An introduction to Python for non-engineers
Introduction to Python Django (2) Mac Edition
[AWS SAM] Introduction to Python version