What are List Operations in Python | EP-18 List Operations in Python | List Methods In Python
Adding Elements to a Python List
Method 1: Using append() method
Elements can be added to the List by using the built-in append() function. Only one element at a time can be added to the list by using the append() method, for the addition of multiple elements with the append() method, loops are used. Tuples can also be added to the list with the use of the append method because tuples are immutable. Unlike Sets, Lists can also be added to the existing list with the use of the append() method.
Python program to demonstrate
Addition of elements in a List
Creating a List
List = []
print(List)
Addition of Elements
in the List
List.append(1)
print(List)
Adding elements to the List
using Iterator
for i in range(1, 11):
List.append(i)
print(List)
Adding Tuples to the List
List.append((5, 6))
print(List)
Addition of List to a List
List2 = [ 'cybrosys']
List.append(List2)
print(List)
Method 2: Using insert() method
append() method only works for the addition of elements at the end of the List, for the addition of elements at the desired position, insert() method is used. append() which takes only one argument, the insert() method requires two arguments(position, value).
Python
Python program to demonstrate
Addition of elements in a List # Creating a List
List = [1,2,3,4]
print(List)
Addition of Element at
specific Position
(using Insert Method)
List.insert(3, 12)
print(List)
Method 3: Using extend() method
Other than append() and insert() methods, there’s one more method for the Addition of elements, extend(), this method is used to add multiple elements at the same time at the end of the list.
Python
Python program to demonstrate
Addition of elements in a List
Creating a List
List = [1, 2, 3, 4]
print(List)
Addition of multiple elements
to the List at the end
(using Extend Method)
List.extend([8, 'cybrosys', 'technologies'])
print(List)
Method 1: A list can be reversed by using the reverse() method in Python.
Python
Reversing a list
mylist = [1, 2, 3, 4, 5, 'cybrosys', 'Python']
mylist.reverse()
print(mylist)
Method 2: Using the reversed() function:
The reversed() function returns a reverse iterator, which can be converted to a list using the list() function.
Python
my_list = [1, 2, 3, 4, 5,6,7,8,9,10]
reversed_list = list(reversed(my_list))
print(reversed_list)
Removing Elements from the List
Method 1: Using remove() method
Elements can be removed from the List by using the built-in remove() function but an Error arises if the element doesn’t exist in the list. Remove() method only removes one element at a time, to remove a range of elements, the iterator is used. The remove() method removes the specified item.
Python program to demonstrate
Removal of elements in a List
Creating a List
List = [1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12]
print(List)
Removing elements from List
using Remove() method
List.remove(5)
print(List)
Creating a List
List = [1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12]
Removing elements from List
using iterator method
for i in range(1, 8):
List.remove(i)
print(List)
Method 2: Using pop() method
pop() function can also be used to remove and return an element from the list, but by default it removes only the last element of the list, to remove an element from a specific position of the List, the index of the element is passed as an argument to the pop() method.
Python
List = [1, 2, 3, 4, 5]
Removing element from the
Set using the pop() method
List.pop()
print(List)
Removing element at a
specific location from the
Set using the pop() method
List.pop(2)
print("\nList after popping a specific element: ")
print(List)
#python #listoperations #pythonprogramming #pythonlists #listmethods #pythontutorial #coding #programming #tutorial #learnpython #pythonforbeginners #datascience #machinelearning #datastructures #algorithms #ListOperations #Coding #TechEducation #DevTutorials #ListMethods #TechEducation
Connect With Us:
—————————————
➡️ Website: https://www.cybrosys.com/
➡️ Email: info@cybrosys.com
➡️ Twitter: / cybrosys
➡️ LinkedIn: / cybrosys
➡️ Facebook: / cybrosystechnologies
➡️ Instagram: / cybrosystech
➡️ Pinterest: / cybrosys