How to Append Elements to a List in Python

How to Append Elements to a List in Python

In Python, lists are dynamic data structures that allow us to add elements in various ways. Python comes with a number of built-in techniques for extending or appending lists. These methods not only enable us to add individual elements but also allow for appending multiple elements or even entire iterables to a list.

Complete Python Course with Advance topics:-Click here

image-4 How to Append Elements to a List in Python

Here, we will discuss three popular methods for appending elements to a list:

  1. append(elmt): Adds a component to the list’s end.
  2. insert(index, elmt): Inserts an element at a specified index.
  3. extend(iterable): Adds items from an iterable to the list to make it longer.

1. append(elmt)

One element is added at the end of the list using the append() method. This is particularly useful when you want to grow a list incrementally.

Example:

names = ["Joseph", "Peter", "Cook", "Tim"]

print('Current names List is:', names)

new_name = input("Please enter a name:\n")
names.append(new_name)  # Using the append() method

print('Updated names List is:', names)

Output:

Current names List is: ['Joseph', 'Peter', 'Cook', 'Tim']
Please enter a name:
Devansh
Updated names List is: ['Joseph', 'Peter', 'Cook', 'Tim', 'Devansh']

2. insert(index, elmt)

An element can be added at a specified index using the insert() method. This is particularly useful when you need precise control over the position of the new element.

Example:

list1 = [10, 20, 30, 40, 50]

print('Current Numbers List:', list1)

list1.insert(3, 77)  # Inserting 77 at index 3
print("The list after first insertion:", list1)

n = int(input("Enter a number to add to the list:\n"))
index = int(input("Enter the index to add the number:\n"))

list1.insert(index, n)  # Adding user-specified element at the given index
print('Updated Numbers List:', list1)

Output:

Current Numbers List: [10, 20, 30, 40, 50]
The list after first insertion: [10, 20, 30, 77, 40, 50]
Enter a number to add to the list:
45
Enter the index to add the number:
1
Updated Numbers List: [10, 45, 20, 30, 77, 40, 50]

3. extend(iterable)

The extend() method allows you to add elements from an iterable (e.g., another list, tuple, or string) to the end of the list. Unlike append(), which adds the entire iterable as a single element, extend() adds each element of the iterable individually.

Example:

list1 = [10, 20, 30]

list1.extend(["52.10", "43.12"])  # Extending with a list
print("After extending with a list:", list1)

list1.extend((40, 30))  # Extending with a tuple
print("After extending with a tuple:", list1)

list1.extend("Apple")  # Extending with a string
print("After extending with a string:", list1)

Output:

After extending with a list: [10, 20, 30, '52.10', '43.12']
After extending with a tuple: [10, 20, 30, '52.10', '43.12', 40, 30]
After extending with a string: [10, 20, 30, '52.10', '43.12', 40, 30, 'A', 'p', 'p', 'l', 'e']

Key Differences Between Methods

Method Adds at Position Adds Multiple Elements Accepts Iterable
append(elmt) End of the list No No
insert(index, elmt) Specified index No No
extend(iterable) End of the list Yes Yes

Download New Real Time Projects :-Click here
PHP PROJECT:- CLICK HERE


how to append elements to a list in python using for loop
how to append multiple elements to a list in python
how to append multiple lists to a list in python
how to append elements to a list in python
how to append elements to a list in python another list
how to append elements to an empty list in python
how to append an item to a list in python
how to add an element to a list in python
how to add elements to an empty list in python
how to insert an element to a list in python
how to append elements to a list in python example
how to append elements to a list in python w3schools
how to append elements to a list in python using for loop

Post Comment