Python | Program for Adding, removing elements in the list

Here, we are going to learn how to add elements to the list, how to remove elements from the list in Python? By IncludeHelp Last updated : June 21, 2023

Given a list of the elements and we have to add/remove elements in/from the list in Python.

Python List append() Method

The append() method is used to add/append an object (which will be passed in method as parameter) to the list.

Syntax:

 list.append(element)

Here,

  • list - is the name of the list.
  • append() - is the method name, that is used to add element/object to the list.
  • element - is an element (which is considered as on object or element) to be added in the list.

Python List pop() Method

The pop() method is used to remove/pop an object from the list.

Syntax:

 list.pop()

Here,

  • list is the name of the list.
  • pop() is the method name that is used to remove last element from the list.

Python program for adding, removing elements in the list

# Declaring a list with integer and string elements
list = [10, 20, 30, "New Delhi", "Mumbai"]

# printing list
print "List elements are: ", list

# adding elements 
list.append (40)
list.append (50)
list.append ("Chennai")

# printing list after adding elements
print "List elements: ", list

# removing elements
list.pop () ;
# printing list
print "List elements: ", list
# removing elements
list.pop () ;
# printing list
print "List elements: ", list

Output

List elements are:  [10, 20, 30, 'New Delhi', 'Mumbai']
List elements:  [10, 20, 30, 'New Delhi', 'Mumbai', 40, 50, 'Chennai']
List elements:  [10, 20, 30, 'New Delhi', 'Mumbai', 40, 50]
List elements:  [10, 20, 30, 'New Delhi', 'Mumbai', 40]

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.