Python List append() Method

In this article, we will learn about the Python List append() function, the syntax of Python List append(), some examples in which we use the append() function, and also its code implementation in python programs how we add the element using append() function and return type of python list append() function.

Python List append()

Python list append(), is a method or function which is used to add elements at the end of the list. it basically doesn’t create a new list but it actually modifies the existing one. It accepts a single element (could be any python object) like an integer, string, list, or floating number. it does not return the value.

Note: This function doesn’t return anything and it only changes the original list.

Return type: None

Syntax:

list.append(item)

Here item can be an integer, list, or string.

Example 1

In this example we are taking a list of( string datatype) and we simply use the append() function to append another element which is (“college Dekho”) string.

Code Implementation:

# your code goes here

# your code goes here

our_list=["prep","bytes"]

our_list.append("college Dekho")

print(our_list)

Output:

['prep', 'bytes', 'college Dekho']

Explanation:
In the above python program we have a list and the name of the list is ‘our_list’ if we observe the list that it consists of two elements of the string(data type). One is “prep” and another is “bytes”. Now we add another string, "college Dekho” so we use the append function, which adds this string at the end of our list(our_list).
After this, we print the (our_list) and we observe on the output screen that the string
( “college Dekho”) has been added at the end of the list(our_list).

Example 2

In this example we have taken 2 lists and we append one list into another by using append() function.

Code Implementation:

# your code goes here


orginal_list=["college","dekho"]

other_list=[1,2,3]


orginal_list.append(other_list)

print(orginal_list)

Output:

['college', 'dekho', [1, 2, 3]]

Explanation:
In the above python program we have two lists. List 1 name as orginal_list and list2 name as other_list.if we observe that list1 consists only of string elements which are (“college”, “Dekho”) and list2 which have integer values (1,2,3).so this is an example of a nested list in which we add one list into another by using the append function we simply pass the other_list as an argument in append() function. after this, we print the original list.
On output, we observe the (other_list) added at the end of the original list.

Summary
The append() function in Python is used to add an element to the end of a list. It takes a single argument, which is the element to be added, and modifies the list in place, so it does not return a new list.

For example:
We have a list name L1 which has an integer value and we use the append function to append (100) in list L1.
L1=[10,20,30,40,50,60,70,80,90]

L1.append(100)

Here 100 is added at the end of list L1.

Now list L1 becomes after using append function-> [10,20,30,40,50,60,70,80,90,100]

Leave a Reply

Your email address will not be published. Required fields are marked *