Home » Python programs

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?
Submitted by IncludeHelp, on July 20, 2018

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.

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]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.