×

How to Add Elements in List in Python?

How to Add Elements in List in Python

We all are aware of the wide spectrum of applications where the data structures of Python are used. Be it lists, tuples, or dictionaries somewhere or the other, three of them are contributing significantly to accomplish our tasks.

Here, first, we will have a glance at a brief introduction of lists and then try to understand how we can add elements in lists with the help of examples-

So, let’s begin-

The list is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets [ ]. Lists are mutable which means their values can be changed. An example of a list is-

list1 = [23,’Apple’,3.5,’Mangoes’]

The values present in a list can be accessed through indexing. For instance, list1[1] will return the value ‘Apple’.

In this article, we will discuss how we can add elements to list in Python using the following methods-

  1. Insert()
  2. Append()
  3. Extend()
  4. List Concatenation

So, let’s have a look at the first method-

INSERT()

When we are using the insert() method to add elements to our list, we must specify the position that is the index where we would like to add the element and the value of our element.

The following program illustrates the same-

 #creating list
 list=[1,5.6,'New York','Hong-Kong',21,10.4]
 print("The created list is {}".format(list))
 print("The type is {}".format(type(list)))
 #accessing values
 print("The value at index 0 is {}".format(list[0]))
 print("The value at index 2 is {}".format(list[2]))
 print("The value at index 3 is {}".format(list[3]))
 #applying insert()
 list.insert(0,'London')
 list.insert(1,11)
 list.insert(3,15.5)
 print(list) 

INPUT-

How to Add Elements in List in Python

OUTPUT-

How to Add Elements in List in Python

In the output, we can observe the following things-

  1. It prints the list that we provided.
  2. Then the data type is printed with the help of type().
  3. Next, we have accessed the values present at the 0, 2nd, and 3rd index.
  4. And finally, the inserted values are displayed, we can see that the values are present at the specified positions.

The second method which we will discuss is append()-

APPEND()

When we are using the append() method to add elements to our list, we have to specify only the value of our element since it adds the value at the end of our list.

Following program illustrates the same-

 #creating list
 list=[1,5.6,'New York','Hong-Kong',21,10.4]
 print("The created list is {}".format(list))
 print("The type is {}".format(type(list)))
 #accessing values
 print("The value at index 0 is {}".format(list[0]))
 print("The value at index 2 is {}".format(list[2]))
 print("The value at index 3 is {}".format(list[3]))
 #applying append()
 list.append('London')
 list.append(11)
 list.append(15.5)
 print(list) 

INPUT-

How to Add Elements in List in Python

OUTPUT-

How to Add Elements in List in Python

In the output, we can observe the following things-

  1. It prints the list that we provided.
  2. Then the data type is printed with the help of type().
  3. Next, we have accessed the values present at the 0, 2nd, and 3rd index.
  4. And finally, the appended values are displayed which are ‘London’, 11 and 15.5

The third method which we will discuss is extend().

EXTEND()

When we are using the extend() method to add elements to our list, we have to specify the value of our element since it adds the value at the end of our list and iterates over its argument.

The following program illustrates the same-

 #creating list
 list=[1,5.6,'New York','Hong-Kong',21,10.4]
 print("The created list is {}".format(list))
 print("The type is {}".format(type(list)))
 #accessing values
 print("The value at index 0 is {}".format(list[0]))
 print("The value at index 2 is {}".format(list[2]))
 print("The value at index 3 is {}".format(list[3]))
 #applying extend()
 list.extend('London')
 list1=[11,12.5,'Canada']
 list.extend(list1)
 print(list) 

INPUT-

How to Add Elements in List in Python

OUTPUT-

How to Add Elements in List in Python

In the output, we can observe the following things-

  1. It prints the list that we provided.
  2. Then the data type is printed with the help of type().
  3. Next, we have accessed the values present at the 0, 2nd, and 3rd index.
  4. And finally, we can see when we pass the string ‘London’ as an argument in extend(), it takes each character of the string as an element and that’s how the list is extended but when we pass a list in it, it simply adds the element of the parameter list at the end.

The last method which we will discuss is adding elements in the list using concatenation-

List Concatenation

When we are using list concatenation to add elements in our list, we create two lists and then concatenate or merge them using the '+' operator to obtain a single list that contains elements of both.

The following program illustrates the same-

 #creating list
 list=[1,5.6,'New York','Hong-Kong',21,10.4]
 print("The created list is {}".format(list))
 print("The type is {}".format(type(list)))
 #accessing values
 print("The value at index 0 is {}".format(list[0]))
 print("The value at index 2 is {}".format(list[2]))
 print("The value at index 3 is {}".format(list[3]))
 #applying concatenation()
 list1=[11,12.5,'Canada']
 list2=list+list1
 print(list2) 

INPUT-

How to Add Elements in List in Python

OUTPUT-

How to Add Elements in List in Python

In the output, we can observe the following things-

  1. It prints the list that we provided.
  2. Then the data type is printed with the help of type().
  3. Next, we have accessed the values present at the 0, 2nd, and 3rd index.
  4. And we can see when we print list2, it is a list that contains elements of both list and list1.

So, in this article, we discussed the various methods of adding elements to a list in Python.


Related Topics

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

How to check data type in python

The data type represents the nature of a variable; it determines what kind of data it can store and what operations can be carried out on it. There are five...

4 minutes read.

How to comment multiple lines in python?

How to comment multiple lines in python There are two qualities that come up with every good and successful programmer- i) Indenting the code ii) Using comments in the code Let us see how...

4 minutes read.

Python Statistical Module

Python Statistical Module The Python statistical module provides various functions that we can use in our Python program, to perform mathematical statistics operations on the numerical data given to us. The...

8 minutes read.

Colors in Python

Adding colour to your visualisations will help them come to life. Even if you know the colours you want to use, picking good ones and putting them into practise might...

4 minutes read.

Managing Multiple Python Versions With pyenv

Introduction If you had ever wished to assist to an application that makes use of several different Python versions but weren't sure whether you would quickly test them all? Do you...

4 minutes read.

Make Notepad using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

5 minutes read.

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

Conditional Statements in python

In this article, we will learn about conditional statements and how to apply conditional statements in Python. A decision-making statement is another name for a conditional statement. Decision-making is an important...

5 minutes read.

Python range() function

Python range() function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax range(stop)        or range(start, stop[, step]) Parameter start: It is...

1 minute read.

Python whois

What is whois? Whois is a protocol used to identify the owner of the registered domain name. It is a querying database that is used to record the registered users. WHOIS is...

4 minutes read.

Python Program for Linear Search

Introduction We employ specific algorithms to efficiently carry out our responsibilities, such as searching for an element in a given data structure. These algorithms fall under the category of searching algorithms....

4 minutes read.

Python callable() Function

Python callable() Function The callable() function returns a boolean value ‘True’ if the specified object is callable, else it returns False. Syntax callable(object) Parameter Object:  The object parameter represents the value to test if it is callable...

1 minute read.

Allocate a minimum number of pages in python

You have given a sorted array of size n which represents the number of pages in n different books and an integer value which denotes the number of students. We...

4 minutes read.

Python email utils

Python is a popular programming language in this growing world. There are many resources to learn python online without spending a single penny. In this article, we will talk about a...

4 minutes read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

Tensorflow Angular in Python

TensorFlow is an open-source, Python-compatible toolkit for numerical computation that accelerates and simplifies the creation of neural networks and machine learning algorithms. They make the interesting notion that models can be...

6 minutes read.

Python Program to Find the gcd of Two Numbers

Introduction Greatest Common Divisor is the full form of gcd. The greatest common divisor, or GCD, of two numbers, is a value that can exactly divide the two digits and is...

5 minutes read.

Python Letter to Number

Python Letter to Number In this tutorial, we will convert the given letters into numbers using a Python. We will convert the given letter into the letter value as defined in...

3 minutes read.