Different ways to update Python list - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
There are several ways to update the List in Python, let’s see them.
Python list provides the following methods to modify the data.
list.append(value) # Append a value
list.extend(iterable) # Append a series of values
list.insert(index, value) # At index, insert value
list.remove(value) # Remove first instance of value
list.clear() # Remove all elements
If you like to add a single element to the python list then list.append(value) is the best fit for you. The list.append(value) always adds the value to the end of existing list.
a_list = [1, 2, 3, 4]

a_list.append(5)

print(a_list)
Output:
[1, 2, 3, 4, 5]
The append and extend methods have a similar purpose: to add data to the end of a list. The difference is that the append method adds a single element to the end of the list, whereas the extend method appends a series of elements from a collection or iterable.
a_list = [1, 2, 3, 4]

a_list.extend([5, 6, 7])

print(a_list)
Output:
[1, 2, 3, 4, 5, 6, 7]
The insert() method similar to the append() method, however insert method inserts the value at the given index position, where as append() always add the element at the end of the list.
a_list = [10, 20, 40] 
a_list.insert(2, 30 )  # At index 2, insert 30.

print(a_list)
Output:
[10, 20, 30, 40]
If the provided index out of range, then the insert() method adds the new value at the end of the list, and it inserts the new value to the beginning of the list if the given index is too low.
a_list = [10, 20, 30]
a_list.insert(100, 40)
print(a_list)
a_list.insert(-50, 1)
print(a_list)
Output:
[10, 20, 30, 40] 

[1, 10, 20, 30, 40]
The remove(value) method removes the first occurrence of the given value from the list. There must be one occurrence of the provided value, otherwise the Python raises ValueError.
a_list = [1, 2, 3, 4, 5, 4] 

a_list.remove(4)

print(a_list)
Output:
[1, 2, 3, 5, 4]
Removing min and max values from the list.
a_list = [1, 2, 3, 4, 5, 4, 7, 9 , -1] 

a_list.remove(max(a_list)) # removed the max element from the list
a_list.remove(min(a_list)) # removed the min element from the list

print(a_list)
Output:
[1, 2, 3, 4, 5, 4, 7]
The clear() method used to remove all the elements from the list. The same we can also do with del list[:]
a_list = [10, 20, 30, 40]

a_list.clear()

print(a_list)
Output:
[]
Python List Data structure in Depth
More on Python list methods
Happy Learning 🙂

Python List Data Structure In Depth
Python – How to remove duplicate elements from List
How to remove empty lists from a Python List
Different ways to use Lambdas in Python
How to clear all elements from List in Python
Different ways to do String formatting in Python
Python List comprehension usage and advantages
Implementing Stack in Python
What are the different ways to Sort Objects in Python ?
How to Remove Spaces from String in Python
How to access for loop index in Python
Ways to create constructors In Python
How to Create or Delete Directories in Python ?
How to Convert Python List Of Objects to CSV File
Python – Update Records in MySQL Data Database

Python List Data Structure In Depth
Python – How to remove duplicate elements from List
How to remove empty lists from a Python List
Different ways to use Lambdas in Python
How to clear all elements from List in Python
Different ways to do String formatting in Python
Python List comprehension usage and advantages
Implementing Stack in Python
What are the different ways to Sort Objects in Python ?
How to Remove Spaces from String in Python
How to access for loop index in Python
Ways to create constructors In Python
How to Create or Delete Directories in Python ?
How to Convert Python List Of Objects to CSV File
Python – Update Records in MySQL Data Database
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list