×

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

apply() function in python

Pandas: Pandas is a library in python. It is an open source package in python. Pandas in python are used for data cleaning and data analysis. Data frame mainly consists of...

3 minutes read.

Iterators in Python

Introduction In Python, an iterator is defined as an object that enables traversing through all the values of a collection. It contains the countable number of values. The iterator is utilized to...

4 minutes read.

Python Tkinter Tutorial

What is Tkinter? Tkinter is GUI library of Python. When we integrated Tinkter with Python, it provides an easy and quick way to develop GUI applications. It provides the Tk GUI...

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

Anytree Python

Python's anytree module is frequently used within data science-related software. Anytree has a Tolerant License, a Construct File that is public, no errors, no risks, and excellent support. One may...

3 minutes read.

How to Install Scikit-Learn

Sklearn or Scikit-learn is a python library used for machine learning. It contains many features like classification, regression, clustering, and Dimensionality reduction algorithms. Sklearn is used to build machine learning...

3 minutes read.

Python variance() function

Variance The variance is the average of the square deviations from the mean. The variance will measure the spread of the dataset from its mean or median value. The greater the...

4 minutes read.

How to Configure Python Interpreter in Eclipse

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.

How to Update Python?

How to Update Python In this article, we will discuss how we can update Python in our system. For a better understanding, this article will cover all the steps right from installation...

4 minutes read.

Check Palindrome in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

Python Scikit-image | Image Processing Using Scikit-Image

What is Image Processing? The world is defined with images and, every image has its different specialties. An image can contain much-needed information that can be helpful in various ways. The process...

4 minutes read.

Python filter() function

Python filter() function The filter() function constructs an iterator from those elements of iterable for which the parameter ‘function’ returns a Boolean value true. Syntax filter(function, iterable) Parameter function: This parameter represents a function to be run for each item in the...

1 minute read.

Difference between Module and Package in Python

The main difference between the module and the package in Python is that the module can be a simple file in Python that contains the different functions and collection of...

4 minutes read.

What Does the Percent Sign (%) Mean in Python?

In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand. Example: value1 = 8 value2 = 2 remainder =...

4 minutes read.

String indices must be integers in Python

Lists, tuples, and strings are examples of iterable objects in Python whose items or characters can be retrieved by their index numbers. For instance, you might take the following action to...

3 minutes read.

Python Rest API

In this tutorial, we will understand the meaning of API and REST API. We will understand the working of REST API. We will then realize the boundaries of architecture defined...

4 minutes read.

Captcha Code in Python with Example

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

6 minutes read.

Python Lists vs Tuples

The difference between lists and tuples is one of the most frequently asked questions in an interview related to python language. Lists and Tuples are two of Python’s built-in data...

4 minutes read.

Length of Tuple in Python

What is Tuple? Python is a data structure in a python programming language; it is the collection of the objects in a sequence. The tuples are immutable; that is, we cannot...

3 minutes read.