×

List in Python

What is List in Python

In Python, lists are used to store the multiple values in one variable. We can say that list is the collection of similar as well as different types of data. Python contains 4 built-in data types and the list is one among them. Lists are created using square brackets.

Suppose we want to store the age of 10 students in class, instead of creating the 10 different variables, we will create one list and in that list, we can store the age of all 10 students.

What is List in Python

Example 1:

#Program of The list
age = [12, 13, 11, 15, 17, 16, 14, 11, 10, 15]
print(numbers)

Output:

[12, 13, 11, 15, 17, 16, 14, 11, 10, 15]

Example 2:

a= [4,"hello",12.6,'z']
print(a)

Output:

[4,"hello",12.6,'z']

Explanation:

In example 1, we have taken the same type of data in the list. But in example 2, we have taken different data types. As we know that list can store similar as well as different data.

What is an Index?

In the list, the index is the integer value of that location where the data is present in the list.

Example:

Names = [“Yashraj”, “Rohit”, “Akash”, “Shubham”]
What is List in Python

Characteristics of List

  • Lists are mutable, which means we can do changes in the list.
  • We can access the elements of the list with the help of the Index.
  • A list can store any kind of data.
  • The list is ordered.
  • The list is dynamic.
  • The list can be nested.

Accessing List Elements

In a list, every element is associated with a number that number is known as the list index. To access the element from the list, we need the index number of that element. In the list, index number always starts with 0.

Example:

Names = ["Yashraj", "Rohit", "Akash", "Shubham"]
# accessing the item at index 0
print(Names[0])
# accessing the item at index 3
print(Names[3])

Output:

Yashraj
Shubham

Negative indexing in a list

In Python, we can give the negative indexing. The negative index starts from length to the -1, because 0 cannot be negative so it starts with the -1.

Example:

Names = [“Yashraj”, “Rohit”, “Akash”, “Shubham”]
What is List in Python

Example:

Names = ["Yashraj", "Rohit", "Akash", "Shubham"]
# accessing the item at index 0
print(Names[-1])
# accessing the item at index 2
print(Names[-3])

Output:

Shubham
Rohit

Slicing in a list

In Python, it is possible to access particular items or elements from the list using the slicing operator.

Syntax:

slice(start, end, step)
  • Start: Here, you need to mention the index from where you want to start your slicing.
  • End: Here, you need to mention the index where you need to end the slicing
  • Step: Here, you need to mention the step of slicing.

Example:

# List slicing in Python
A = ['h','e','l','l','o']
# elements from index 1 to index 3
print(A[1:3])
# elementsfrom index 3 to end
print(A[3:])
#elementsbeginning to end
print(A[:])

Output:  

['e', 'l']
['l', 'o']
['h', 'e', 'l', 'l', 'o']

Explanation:

In above program,

  • A[1:3]  will return the list with an item from index 1 to index 3.
  • A[3:] will return the list with items from index 3 till the end of the list.
  • A[:]  will return the total list.

Add an element to the list

In the list, we can add the element, which means if there are already some elements in the list then we can also add a new element to the existing list.

There is various methods to add the element in the list, one method is using append and we can do this using the extend method also.

Append()

In the append method, we will add the data at the end of the list.

Example:

A = [21, 11, 34, 44]
print("Before Append:", A) 
# using append
A.append(32)
print("After Append:", A)

Output:

Before Append: [21, 11, 34, 44]
After Append: [21, 11, 34, 44, 32]

Extend()

Using the extend method; we will add one list to another list. Like if we have two lists then with the help of the extend function, we can convert the two lists into one list.

Example:

A1 = [22, 33, 55]
print("List1:", A1)
A2 = [43, 66, 88]
print("List2:", A2)
# join two lists
A1.extend(A2)
print("List after append:", A1) 

Output:

List1: [22, 33, 55]
List2: [44, 66, 88]
List after append: [22, 33, 55, 44, 66, 88]

Related Topics

Nested Tuple in Python

If you are a Python learner, this page contains all the information that helps you know about nested tuple and how to access it in python. What is a Tuple? Multiple items...

4 minutes read.

How to append an Array in Python

A group of objects kept at adjacent memory regions is known as an array. It is a container with a set capacity for a certain number of things, all of...

7 minutes read.

Python divmod() function

Python divmod() function The divmod() function in Python returns a tuple containing the quotient  and the remainder when parameter ‘a’ (divident) is divided by parameter ‘b’ (divisor). Syntax: divmod(a, b) Parameter a: This parameter represents a number you...

1 minute read.

Python Setup guide and installation

Python Installation guide Step by Step Python is widely used high-level programming language. The first version of Python was launched in 1991. Since then, Python has been gaining popularity. It is considered as...

4 minutes read.

Nested List in Python

The list is an inbuilt sequential data type in Python. It is a very useful and frequently used data type. A list can store any number of data items of...

4 minutes 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.

Get Bounding Box Co-ordinates Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

6 minutes read.

Traverse Dictionary in Python

Traversing a dictionary is visiting each and every step in the dictionary, which is also called iterating the dictionary In Python, dictionaries are a useful and commonly used data structure in...

3 minutes read.

How to Create an Array in Python?

How to Create an Array in Python In the previous article, we have discussed how to declare an array in Python. So, let's have a quick revision of that, and then we...

5 minutes read.

Python vs PHP

Python Python is a high-level, object-oriented, interpreted language that is used to create independent program and algorithms for a variety of applications. It has a large library support base. In 1990,...

3 minutes read.

How to Program in Python on Raspberry pi?

Introduction to Python A popular programming tool with simple, complete novice syntax is Python structure of paragraphs, phrases, and words. Due to its widespread use, this has a large community that...

4 minutes read.

Best Python AI Projects

Artificial consciousness is advancing quickly, from Chabot's to self-driving vehicles. Because of the various advantages and development presented by AI, numerous enterprises have begun searching for AI-fueled applications. Thus, there...

7 minutes read.

Standard GUI Unit Converter using PyQt5 in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

How to get current date in python?

How to get current date in python? In Python programming language, date and time are not just a data form, but it is possible to import a method called datetime to operate...

3 minutes read.

Python Basics

In this tutorial, we will learn about the basics of Python, a very famous programming language. This tutorial will help you understand the language better if you start with python....

8 minutes read.

Python Indentation

Overview The spaces at the start of a code line are known as an indentation in Python programming. Indentation is only used for readability in other programming languages like C, C++,...

8 minutes read.

Python String join() method

Python String join() method The String.join() method in Python concatenates each element of an iterable (such as list, string and tuple) to the given string and returns the concatenated string. Syntax string.join(seq) Parameter Seq: This...

1 minute read.

Python String Lowercase

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Syntax Error Invalid Syntax

Python is renowned for its simple and direct syntax. However, we might come across some things that Python doesn't allow if we are learning Python for the very first time or if...

14 minutes read.

How to convert integer to float 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...

5 minutes read.