×

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

What are the Purposes of Python?

Python is a high-level programming language that is simple to use and easy to write, and Python is a beginner-friendly programming language. A beginner often prefers to start with Python...

6 minutes read.

Python for loop

 Python for loop A for loop in Python executes a block of code for a specified number of times, based on a given sequence. The for loop in Python is different than any other...

3 minutes read.

Speech Recognition Module in Python

Speech Module in Python: Converting text to speech, known as Speech Synthesis, this process is the computer-generated recreation of human speech. This module converts the human language text into human-like...

8 minutes read.

Python Set symmetric_difference_update() method

Python Set symmetric_difference_update() method The set.symmetric_difference_update() method in Python updates the original set by removing items that are present in both sets and inserting the other items of the set. Syntax set.symmetric_difference_update(set1) Parameter set- This parameter represents the first...

2 minutes read.

Python Write Excel File

Python Write Excel File The Python xlwt module is used to write an excel file and perform multiple operations on it. It can be used to write text, numbers, and formulas for multiple...

3 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

Scraping data in python

Data scraping is a technique in which one program extracts a set of data from the output of another program. Web scraping is the most common application of this technique....

6 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.

Python Set update() method

Python Set update() method The set.update() method in Python updates the current set, by adding items from another set. If an element is common in both the sets, only one appearance of this item...

1 minute read.

Returning Multiple Values in Python

Python is considered a general-purpose programming language; it is a high-level programming language that is not much difficult and easier to learn. It is rich in libraries that can be...

3 minutes read.

Python type() Function

Python type() Function The type() function in Python returns the type of an object. The return value is a type object and generally the same object as returned by object.__class__. Syntax class type(object)      ...

1 minute read.

Import Function in Python

In Python programming language, the import keyword plays an important role in the whole code because it makes one module make available in another module. Import is used to structure...

3 minutes read.

Python String find() method

Python String find() method The string.find() method in Python finds the first occurrence of the specified value and returns the lowest index in the string if the substring sub is found else it...

2 minutes read.

Python data science course

What is meant by Data Science? When processing raw, structured, and unstructured data utilizing various technologies, algorithms, and the scientific method, data science is a detailed study of the enormous quantity...

4 minutes read.

Exclusive OR in Python

In Python, the exclusive OR (XOR) operator is represented by the caret symbol (^). It compares each bit of the first operand to the corresponding bit of the second operand,...

3 minutes read.

Cross Validation in Sklearn

Data scientists can benefit from cross-validation in machine learning in two key ways: it can assist in minimising the amount of data needed and ensure the artificial intelligence model is...

14 minutes read.

Reverse a sentence In Python

Introduction The built-in reverse() function is not supported by the Python string library. As we know, a Python string is defined as a set of Unicode characters and Python provides various...

4 minutes read.

Python BS4 Code

Python BS4 Code The BS4 stands for BeautifulSoup version 4.x. The BeautifulSoup is a Python library which is used for pulling out data of the HTML & XML files using the...

14 minutes read.

Data Structures and Algorithms Using Python | Part 1

Data Structures: Data Structure is defined as a way to organize and store the data so that we can access the data and work more efficiently. Data structures also describe the...

18 minutes read.

Python Stack

Python Stack: The work Stack is defined as arranging a pile of objects or items on top of another. It is the same method of allocating memory in the stack...

10 minutes read.