
👉Python append() Method Explained with Examples
Python append: The append() method in Python is a fundamental tool used to add new elements to an existing list. Python append It efficiently inserts items at the end of the list, making it a preferred method for dynamic data manipulation.
👉What is the append() Method in Python?
Python append: The append() function adds a single element to the end of a list. It modifies the original list directly without returning a new one.
👉Python append Syntax:
list.append(object)
Parameters:
- object: The item to be added. It can be an integer, string, float, or even another list or object.
Note: The append() method returns None and modifies the list in place.
👉How to Use the append() Method in Python
There are two common ways to create and populate a Python list:
- Using list comprehension.
- Using append() with a for loop.
👉Example Using append() with for Loop:
import math
def calc_sqr_root(b_list):
bop = []
for number in b_list:
bop.append(math.sqrt(number))
return bop
base_list = [4, 9, 100, 25, 36, 49, 81]
print(“The original list is:”, base_list)
print(“The list with square roots is:”, calc_sqr_root(base_list))
Output:
The original list is: [4, 9, 100, 25, 36, 49, 81]
The list with square roots is: [2.0, 3.0, 10.0, 5.0, 6.0, 7.0, 9.0]
👉Example Using List Comprehension:
import math
def calc_sqr_root(b_list):
return [math.sqrt(number) for number in b_list]
base_list = [4, 9, 100, 25, 36, 49, 81]
print(“The original list is:”, base_list)
print(“The list with square roots is:”, calc_sqr_root(base_list))
Output:
The original list is: [4, 9, 100, 25, 36, 49, 81]
The list with square roots is: [2.0, 3.0, 10.0, 5.0, 6.0, 7.0, 9.0]
👉How Does the append() Method Work?
- The append() method adds elements to the next available index in the list.
- Python lists maintain the order of elements, making it easy to access data via indexing.
👉Example Using append() to Add Elements:
baselist = [‘P’, ‘Y’, ‘3’, ‘4.2’, ‘T’]
print(“Original list:”, baselist)
baselist.append(‘n’)
print(“List after appending:”, baselist)
print(“Element at index 5:”, baselist[5])
Output:
Original list: [‘P’, ‘Y’, ‘3’, ‘4.2’, ‘T’]
List after appending: [‘P’, ‘Y’, ‘3’, ‘4.2’, ‘T’, ‘n’]
Element at index 5: n
👉Adding Elements Without Using append()
You can also insert elements into a list by using the len() function to identify the last index position.
👉Example Without Using append():
base_list = [2, 4, 6]
print(“List before insertion:”, base_list)
base_list[len(base_list):] = [10]
print(“List after insertion:”, base_list)
Output:
List before insertion: [2, 4, 6]
List after insertion: [2, 4, 6, 10]
👉Defining a Stack Using append() and pop()
A stack follows the Last In First Out (LIFO) principle, where:
- append() acts as the push operation.
- pop() acts as the pop operation to remove elements.
👉Example of Stack Operations:
# Initialize the stack
GGGstack = []
GGGstack.append(100)
GGGstack.append(2333)
GGGstack.append(50000)
print(“Stack after additions:”, GGGstack)
print(“Popped item:”, GGGstack.pop())
print(“Popped item:”, GGGstack.pop())
print(“Popped item:”, GGGstack.pop())
Output:
Stack after additions: [100, 2333, 50000]
Popped item: 50000
Popped item: 2333
Popped item: 100
👉What is the extend() Method in Python?
The extend() method is used to add multiple elements from an iterable like lists, tuples, or dictionaries to an existing list.
👉Python append Syntax:
list.extend(iterable)
👉Key Differences Between append() and extend()
Feature | append() | extend() |
Adds single element | Yes | No |
Adds multiple elements | No | Yes |
Accepts iterable data types | No | Yes |