Python program to add elements to a list


In this article, we will learn how to add elements in list. In python there many ways to add elements into a list by using different operators. “operators” are special symbols that carries out arithmetic or logical computation. The value that operator operates is known as “operand”. There are many types of operators used in python like” +” for add element, “-” to slicing element , ”*” for repeating the element and etc.

List in Python

A list is a data structure in python that is mutable or changeable, ordered sequence of elements. List are used to store multiple in single variable. List consists of four built in data type in python used to store collection of data. And the other three are Tuple, set, dictionary.

Example

The following example creates three lists - list1, list2 and list3. List1 contains numbers 1, 2, 3 and 4; List2 contains strings "keshav", "mohan" and "govind"; List3 combines elements from both the lists containing a number followed by a string.

list1 = [1,2,3,4]
print (list1)
list2 = ["keshav","mohan","govind"]
print (list2)
list3 = [1,"keshav",2,"mohan"]
print(list3)

Output

[1, 2, 3, 4]
['keshav', 'mohan', 'govind']
[1, 'keshav', 2, 'mohan']

Before learning how to add elements of a list let’s first learn how to create a list. This will give a quick revision of the basic concepts.

Creating a List

We can create a list, by placing elements inside square bracket [ ], separated by commas. Let’s learn it by giving an example.

Example

The following example shows how to craete a list. Here, we are taking three names: "alina", "akash", and "arjun".

list= ["alina","akash", "arjun"]
print(list)

Output

Executing the above given program yields the following list of names.

['alina', 'akash', 'arjun'] 

Elements in List

Lists are mutable, which means this element can be changed, added and subtracted (sliced).

We use the operator = to change an item or element in list.

Example

The following example defines the names.

names=["ann","yash","maria"]
print(names)

Output

Upon executing the above program, we get the following output, it prints the elements present in the list as follows.

['ann', 'yash', 'maria']

Example

Here the operator = is used to change the element. In this example step 1 shows that names list is ann, yash , maria but in step 2 we had change the last name “maria “with “ mike” by using “=”operator.

names = ["ann","yash","maria"]
print(names)
names [2] = "mike"
print (names)

Output

Executing the above program gives the following output, thus changing the name “Maria” with “Mike” using = operator

['ann', 'yash', 'maria']
['ann', 'yash', 'mike']

Adding Elements In List

We can add item to a list by using the append(), extend(), insert(), concatenation().

Python allows users to add elements to a list in various ways. The append() method adds an element to the end of the list, whereas extend() appends multiple items at once.

Insert() can be used to insert an item at any given index, while concatenation() combines two lists together into one. All four of these methods are useful for adding elements to a Python list.

Using Concatenation Operator ("+")

Concatenation is the process of combining two or more strings together in Python. This can be done using the '+' operator, or by using formatting functions such as str.format(), f-strings, and format specifiers. Concatenation allows us to create longer strings by joining shorter strings together.

Example

The example given below adds two lists together. The first list, names1, contains the names Ann, Yash and Maria. The second list, names2, contains the names John, Andrew and Robin.

names1=["ann","yash","maria"]
names2=["john","andrew","robin"]
print(names1+names2)

Output

['ann', 'yash', 'maria', 'john', 'andrew', 'robin']

Using Append() Method

Append() is a built-in method in Python that adds an element to the end of a list. It takes one argument, which can be any data type such as an integer, string, or another list.

This method does not return anything; it simply modifies the original list by adding the new element. Append() is a useful method for adding items to lists in Python.

Example

This example adds the string "cherry" to a list of fruits called fruit. After adding it, the contents of the list are printed out which now includes "apple", "mango", "banana" and "cherry".

fruit=["apple", "mango", "banana" ]
a="cherry"
fruit.append(a)
print ("the updated list :",fruit) 

Output

the updated list : ['apple', 'mango', 'banana', 'cherry']

In this program, we had a list which consisted of three fruit: ['apple', 'mango', 'banana'] and we wanted to add fourth fruit name “cherry” at the end element of list . Here we have used append()as an operator and we got output as : ['apple', 'mango', 'banana', 'cherry'].

Using Extend() Method

. Extend() is a function in Python that adds elements from one list to another. It appends all the items in an iterable to the end of the list, extending it. It takes an iterable as input and extends the list with its individual elements.

The original order of items is maintained when they are added to the existing list. This method does not return any value but updates existing lists in memory with new values.

Example

In this example, we are using extend() function to extend and add an element to the list fruits and output is ['apple', 'mango', 'banana', 'c', 'h', 'e', 'r', 'r', 'y']

fruit=["apple", "mango", "banana" ]
a="cherry"
fruit.extend(a)
print("the updated list :",fruit)

Output

the updated list : ['apple', 'mango', 'banana', 'c', 'h', 'e', 'r', 'r', 'y']

Using Insert() Method

Insert() is a function in python that inserts an element at the specified position. It takes two parameters: the index of the element before which to insert, and the item to be inserted.

This can be used to add elements into a list or any other data structure such as tuples and dictionaries. It's important to note that inserting an element shifts all existing elements after it one position ahead in memory.

Example

In the following example, we have a list which consistes of three fruits: ['apple', 'mango', 'banana'] and since we want to add “orange” at a specific position, we used the operator insert(). By this insert() operator we can add any element at any index.

fruits=['apple','banana','cherry']
fruits.insert(1,'orange')
print(fruits)

Output

['apple', 'orange', 'banana', 'cherry']

Conclusion

In this article, we have briefly explained how to add the element in list using python. We have used four different methods insert(), concatenation(), append(), extend(). Each function has a different way of completing the task that is Concatenation() is used for combining two list . Append() is used for adding element at the end of list . extend() is used to add and extend element adding in list. insert() is used for add element at any index.

Updated on: 21-Apr-2023

478 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements