Home » Python

Python List | append() Method with Example

Python List append() Method with Example: In this article, we are going to learn about list.python() Method with Example.
Submitted by IncludeHelp, on August 14, 2018

list.append() Method

append() is an inbuilt method in Python, which is used to add an element/item to the list. append() adds the element at the end.

Syntax:

    list.append(element)

Here,

  • list is the name of the list.
  • element is an item/element to be appended.

Return type: Method append() does not return any value, it just modifies the existing list.

Example:

# Python program to demonstrate
# an example of list.append() method 

# list
cities = ['New Delhi', 'Mumbai']

# print the list
print "cities are: ",cities

# append two more cities
cities.append('Bangalore');
cities.append('Chennai');

# print the updated list
print "cities are:",cities

Output

    cities are:  ['New Delhi', 'Mumbai']
    cities are: ['New Delhi', 'Mumbai', 'Bangalore', 'Chennai']

Explanation:

  • Initially there were two elements in the list ['New Delhi', 'Mumbai']
  • Then, we added two more city names (two more elements) - 1) 'Bangalore' and 2) 'Chennai', by using following python statements:
  •     cities.append('Bangalore');
        cities.append('Chennai');
    
  • 'Bangalore' and the 'Chennai' will be added at the end of the list named cities.
  • Then, we printed the updated list; the output will be a new list with 4 elements ['New Delhi', 'Mumbai', 'Bangalore', 'Chennai'].





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.