What are List Operations in Python | EP-18 List Operations in Python | List Methods In Python

Subscribers:
27,900
Published on ● Video Link: https://www.youtube.com/watch?v=aD_7ZN-X4lI



Duration: 0:00
121 views
1


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  




Other Videos By Cybrosys Technologies


2024-07-29Odoo 18 New Features | Expected Features in Odoo 18 | Odoo 18 Upcoming Features | EP-2 Tech Tonic
2024-07-28How to Find the Second Largest Number in a List | EP-20 Find the Second Largest Number in a List
2024-07-28How to Schedule an Interview in Recruitment Software for Free | Best Interview Scheduling Software
2024-07-28Invoice/Bill Wizard in Odoo 18 Purchase Module | Odoo 18 New Features | Odoo 18 Latest Features
2024-07-25Signature Request in Odoo 18 | How to Request an Online Sign to Confirm Orders| Odoo 18 New Features
2024-07-25Pricer Store in Odoo 18 POS | How to Configure Pricer Store in Odoo 18 POS | New features in Odoo 18
2024-07-24What is List Slicing in Python | EP-19 List Slicing in Python | How to Slice Lists in Python
2024-07-24Odoo 18 New Report Layouts & Fonts | How to Create New Report Layouts | New features in Odoo 18
2024-07-23Cloud Storage Application in Odoo 18 | Cloud Storage App in Odoo 18 | Latest Features in Odoo 18
2024-07-23Confirm & Reset Button for Selecting Multiple Companies in Odoo 18 | New Features in Odoo 18
2024-07-22What are List Operations in Python | EP-18 List Operations in Python | List Methods In Python
2024-07-21How to Count Records Using Search Count in Odoo 17 | Odoo 17 Development Tutorials | Search_Count()
2024-07-21How to Restore Project History in Odoo 18 | Latest Features in Odoo 18 | New Features in Odoo 18
2024-07-18How to Configure Inventory Operation Types in Odoo 17 |What are Warehouse Operation Types in Odoo 17
2024-07-18How to Take User Input in a List in Python | EP-17 Taking Integer Input in Python List | Python List
2024-07-17What is List in Python | EP-16 List in Python | How to Create List in Python | Lists Functions
2024-07-17How to Manage Manufacturing Backorders in Odoo 17 | Odoo 17 Manufacturing Tutorials
2024-07-16How to Configure Re Sequence in Odoo 17 Accounting App | Odoo 17 Accounting | Functional Tutorials
2024-07-16How to Handle Missing Keys in Python Dictionaries | Ep-15 Handling Missing Keys in Dictionaries
2024-07-15How to Create a Ticket From Forum Post in Odoo 18 | New & Latest Features in Odoo 18 | Odoo 18 Forum
2024-07-15How to Create Editable Tree View in Odoo 17 | Odoo 17 Development Tutorials | Tree View in Odoo 17