×

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 And Operator

Python's and operator takes two operands that can be either object, Boolean expressions, or both. And operator creates more complex expressions using those operands. Conditions are the common name for...

8 minutes read.

Python exe() function

Python exe() function The exec() function in Python executes the specified Python code and accepts large blocks of code. It supports dynamic execution of Python code where the object must be either a string...

1 minute read.

Python Comment Block

In this tutorial, we will see what comment blocks mean in Python. Further, we will see the commenting methods supported in Python. We will understand the topics deeply with the...

6 minutes read.

__GETITEM__ and __SETITEM__ in Python

These methods are used in assignment operations, unary comparison operations, binary comparison operations and binary operations. These are pre-defined methods that perform many operations on a class instance. Examples like...

3 minutes read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 minutes read.

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

How to Install Matplotlib in Python?

How to Install Matplotlib in Python The speed at which the enormous amount of data is generating has become a huge aid in understanding what's going on currently in the market....

4 minutes read.

Spyder (32-bit) - Free download

Spyder is a free and open-source scientific environment created by and for Python engineers, scientists, and data analysts. It is a robust scientific environment created in Python by and for...

3 minutes read.

Python Generator

Python Generator: A Function is said to be a Python Generator that produces or generates a sequence of results. A Python Generator maintains its native state to work so that...

6 minutes read.

Python System Requirements

Introduction As we know, Python is a popular programming language usually used to write scripts for operating systems. It’s handy adequate for utilizing in web development and application design. In this article,...

2 minutes read.

How to reverse a string in Python

In Python language, we have a few ways to reverse a string. In this article, let us discuss these ways and understand the suitability of these ways in different scenarios...

5 minutes read.

Python Knapsack problem

Python Knapsack problem Before we dig down about Knapsack problems in Python, first let's have a look at what is actually a knapsack problem. What is a knapsack problem? A problem from the...

5 minutes read.

Create the First GUI Application using PyQt5 in Python

GUI: A graphical user interface, or GUI, is present on most personal computers. It provides a simple experience for individuals with various computing skill levels. GUI apps may take more resources...

3 minutes read.

Performing Transaction Using Python MySQL

Transaction A Transaction contains a set of SQL commands used to change the data in the dataset. If we say transaction, it means that the data in the database has changed....

3 minutes read.

Python Sys Stdout

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

How to comment out a block of code in Python

When you are writing code in Python, you may want to document it. You need to mention why a piece of code functions, for instance. Mathematics, algorithms, and intricate business...

4 minutes read.

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python. Face recognition is of great utility in real-world scenarios. It is an extended step...

3 minutes read.

PyDev with Python IDE

What is IDE? Integrated Development Environment is the abbreviation of IDE. It is a coding tool that automates code editing, finding errors and testing. IDE combines all the developer tools into...

3 minutes read.

Python Uses

Python has advanced in recent years to rank among the programming languages that are most often used globally. It is utilized in everything, including machine learning, software testing, and website...

4 minutes read.

Python Deep Copy and Shallow Copy

Assignment statements in Python create bindings between a target and an object, not copies of them. The = operator only makes a new variable that shares the reference to the...

3 minutes read.