Python Tutorial - List Add Element

Python Exercise Lists

In actual development, you often need to update Python lists, including adding elements to the list, modifying elements in the table, and deleting elements. This section first learns how to add elements to the list.

The "Python Sequence" section tells us that you can use the + operator to concatenate multiple sequences; lists are a type of sequence, so you can also use + to concatenate a list.

Example:

name = ["Jason", "Alice", "Mark"]
birthday = [1993, 1988, 2000]
info = name + birthday
print("name =", name)
print("birthday =", birthday)
print("info =", info)
           

The output is:

name = ['Jason', 'Alice', 'Mark']
birthday = [1993, 1988, 2000]
info = ['Jason', 'Alice', 'Mark', 1993, 1988, 2000]

It can be found from the running results that using + will generate a new list, and the original list will not be changed.

+ It is more used to concatenate lists, and the execution efficiency is not high. If you want to insert elements in the list, you should use the following special methods.

Add Element - Python Append Method

The append () method is used to append elements to the end of the list. The syntax of this method is as follows:

listname.append (obj)

Among them, listname represents the list of elements to be added; obj represents the data added to the end of the list, which can be a single element, a list, a tuple, etc.

Example:

name = ["Jason", "Alice", "Mark"]
#Append element
name.append ("Nicole")
print (name)
#Append tuple, the entire tuple is treated as an element
fruit = ("Pineapple", "Mango")
name.append (fruit)
print (name)
#Append list, the entire list is also treated as an element
name.append (['Iven', 'Peggy'])
print (name)
           

The output is:

['Jason', 'Alice', 'Mark', 'Nicole']
['Jason', 'Alice', 'Mark', 'Nicole', ('Pineapple', 'Mango')]
['Jason', 'Alice', 'Mark', 'Nicole', ('Pineapple', 'Mango'), ['Iven', 'Peggy']]

As you can see, when passing a list or tuple to the append method, this method treats them as a whole and adds them to the list as an element, forming a new list containing lists and tuples.

Add Element - Python Extend Method

The difference between extend and append is that extend method does not treat lists or primitives but adds the elements they contain to the list one by one. The syntax of the extend () method is as follows:

listname.extend (obj)

Among them, listname refers to the list of elements to be added; obj represents the data added to the end of the list. It can be a single element, a list, a tuple, etc.

Example:

name = ["Jason", "Alice", "Mark"]
#Append element
name.extend ('Nicole')
print (name)
#Append tuple, the ancestor was split into multiple elements
fruit = ('Pineapple', 'Mango')
name. extend (fruit)
print (name)
#Append list, list is also split into multiple elements
name. extend (['Iven', 'Peggy'])
print (name)
           

The output is:

['Jason', 'Alice', 'Mark', 'N', 'i', 'c', 'o', 'l', 'e']
['Jason', 'Alice', 'Mark', 'N', 'i', 'c', 'o', 'l', 'e', 'Pineapple', 'Mango']
['Jason', 'Alice', 'Mark', 'N', 'i', 'c', 'o', 'l', 'e', 'Pineapple', 'Mango', 'Iven', 'Peggy']

Add Element - Python Insert Method

The append and extend methods can only insert elements at the end of the list. If you want to insert elements somewhere in the middle of the list, you can use the insert method.

The syntax of insert method is as follows:

listname.insert (index, obj)

Among them, index represents the index value of the specified position. Insert method inserts obj at the index element of the listname list.

When inserting a list or ancestor, insert method also treats them as a whole and inserts them into the list as an element, which is the same as append ().

Example:

name = ["Jason", "Alice", "Mark"]
#INSERT ELEMENT
name.insert(1, "Jay")
print (name)
#Inserting tuples, the entire ancestor is treated as an element
fruit = ('Pineapple', 'Mango')
name.insert (2, "Apple")
print (name)
#Insert list, the entire list is treated as one element
name. insert (8, ['Iven', 'Peggy'])
print (name)
#Insert string, the entire string is treated as one element
name.insert (1, "www.freelearningpoints.com")
print (name)
           

The output is:

['Jason', 'Jay', 'Alice', 'Mark']
['Jason', 'Jay', 'Apple', 'Alice', 'Mark']
['Jason', 'Jay', 'Apple', 'Alice', 'Mark', ['Iven', 'Peggy']]
['Jason', 'www.freelearningpoints.com', 'Jay', 'Apple', 'Alice', 'Mark', ['Iven', 'Peggy']]

Hint, insert method is mainly used to insert elements in the middle of the list. If you only want to append elements to the end of the list, I recommend using append and extend method.

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises