Q:

Python | Program for Adding, removing elements in the list

belongs to collection: Python List Programs

0

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

Python List append() Method

It 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

It 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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

# 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]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python List Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Program to print a list using ‘FOR and ... >>
<< Python program to print list elements in different...