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
list.append(item) Here item can be an integer, list, or string.
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.
# your code goes here # your code goes here our_list=["prep","bytes"] our_list.append("college Dekho") print(our_list)
['prep', 'bytes', 'college Dekho']
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).
In this example we have taken 2 lists and we append one list into another by using append() function.
# your code goes here orginal_list=["college","dekho"] other_list=[1,2,3] orginal_list.append(other_list) print(orginal_list)
['college', 'dekho', [1, 2, 3]]
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.
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.
We have a list name L1 which has an integer value and we use the append function to append (100) in list L1.
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]