[Python] How to use list 3 Added

Introduction

We are advancing Python learning according to Learning Roadmap for Python Beginners on Tommy's blog. This time, I will deal with the latter half of [Introduction to Python] Summary of how to use list (Part 1).

Target person

--Those who are studying on Tomi-san's blog --For those who want to get an overview of how to use list

environment

Google Colaboratory

content of study

Use ** Add Method ** to add data to the list

Additional methods

  1. append
  2. extend
  3. insert

Because these are methods

List variable.append()


 I use it.

list_a = [1, 2, 3, 4] #1,2,3,4 list_a list_a.append(5) #list_Add 5 to A at the end print(list_a)



#### **`Execution result`**
```text

[1, 2, 3, 4, 5]         #5 was added last.

Next, let's add the lists together

list_a = [1,2,3,4]
list_b = [5,6]
list_a.append(list_b)
print(list_a)

Execution result


[1, 2, 3, 4, [5, 6]]    #Since append is added as it is[]Will also be added

[] When you don't want to match

list_a = [1,2,3,4]
list_b = [5,6]
list_a.extend(list_b)    #Let's use extend
print(list_a)

Execution result


[1, 2, 3, 4, 5, 6]       #Only the numbers were matched!!

Since extend is a combination of lists, let's create a list

Cannot be used like append

list_a = [1,2,3,4]
list_a.extend(5)

Execution result


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-25c1d760cd29> in <module>()
      1 #Note: Since extend is a combination of lists, create a list and use it.
      2 list_a = [1,2,3,4]
----> 3 list_a.extend(5)

TypeError: 'int' object is not iterable

Will result in an error

insert can be added to the element No in list !!

list_a = [1, 2, 3, 4]
list_b = [5, 6]
list_a.insert(1, list_b)    #Element No.List to 1_b[5, 6]Add
print(list_a)

Execution result


[1, [5, 6], 2, 3, 4]        #Added and printed

insert mystery

list_a = [1, 2, 3, 4]
list_b = [5]

list_a.insert(0, list_b)
print(list_a)

list_a = [1, 2, 3, 4]
list_a.insert(1, list_b)
print(list_a)

list_a = [1, 2, 3, 4]
list_a.insert(2, list_b)
print(list_a)

list_a = [1, 2, 3, 4]
list_a.insert(3, list_b)
print(list_a)

list_a = [1, 2, 3, 4]
list_a.insert(4, list_b)
print(list_a)

Execution result


[[5], 1, 2, 3, 4]    #Element No. 0
[1, [5], 2, 3, 4]    #Element No. 1
[1, 2, [5], 3, 4]    #Element No. 2
[1, 2, 3, [5], 4]    #Element No. 3
[1, 2, 3, 4, [5]]    #Element No. 4

This is OK. Then what is this?

list_a = [1, 2, 3, 4]
list_b = [5]

list_a.insert(-4, list_b)    #Element No. -4
print(list_a)

list_a = [1, 2, 3, 4]
list_a.insert(-3, list_b)    #Element No. -3
print(list_a)

list_a = [1, 2, 3, 4]
list_a.insert(-2, list_b)    #Element No. -2
print(list_a)

list_a = [1, 2, 3, 4]
list_a.insert(-1, list_b)    #Element No. -1
print(list_a)

list_a = [1, 2, 3, 4]
list_a.insert(0, list_b)     #Element No. 0
print(list_a)

Execution result


[[5], 1, 2, 3, 4]    #Element No. -4
[1, [5], 2, 3, 4]    #Element No. -3
[1, 2, [5], 3, 4]    #Element No. -2
[1, 2, 3, [5], 4]    #Element No. -1 Doesn't it come to the end of the element? that?
[[5], 1, 2, 3, 4]    #Element No. 0

I'm not sure right now ...

Next time, I plan to delete clear, pop, remove, del.

reference

[Introduction to Python] Summary of how to use list (Part 1)

Recommended Posts

[Python] How to use list 3 Added
[Python] How to use list 1
How to use list []
Summary of how to use Python list
python3: How to use bottle (2)
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[Algorithm x Python] How to use the list
Python: How to use async with
[Python] How to use Pandas Series
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to use python zip function
[Python] How to use Typetalk API
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to install and use pandas_datareader [Python]
[Python] How to convert a 2D list to a 1D list
[python] How to use __command__, function explanation
[Python] How to use import sys sys.argv
Memorandum on how to use gremlin python
[Python2.7] Summary of how to use unittest
python: How to use locals () and globals ()
How to use __slots__ in Python class
How to use "deque" for Python data
How to use Python zip and enumerate
[Python] Understand how to use recursive functions
How to use regular expressions in Python
[Python2.7] Summary of how to use subprocess
How to use is and == in Python
[Blender x Python] How to use modifiers
[Question] How to use plot_surface of python
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to install Python
How to use Pandas 2
How to use Virtualenv
How to use pytest_report_header
How to install python
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables