You are on page 1of 2

Introduction to Lists

List is a sequence of values of any type. Values in the list are called elements / items. List is enclosed
in square brackets.

Example: a = [1,2.3,"Hello"]

List is one of the most frequently used and very versatile data type used in Python.

How to create a list?


In Python programming, a list is created by placing all the items (elements) inside a square bracket [
], separated by commas. It can have any number of items and they may be of different types
(integer, float, string etc.).

Example:

#empty list

empty_list = []

#list of integers

age = [15,12,18]

#list with mixed data types

student_height_weight = ["Ansh", 5.7, 60]

A list can also have another list as an item. This is called a nested list.

# nested list

Example:- student marks = ["Aditya", "10-A", [ "english",75]]

How to access elements of a list?


A list is made up of various elements, which need to be individually accessed based on the
application it is used for.

There are two ways to access an individual element of a list: -

1) List Index

2) Negative Indexing

List Index:-

A list index is the position at which any element is present in the list. Index in the list starts from 0,
so if a list has 5 elements the index will start from 0 and go on till 4. In order to access an element in
a list we need to use index operator [].

Negative Indexing: -

Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the
second last item and so on.
Adding Element to a List :- Elements from a list can removed using two
methods: -
We can add an element to any list using two
methods : 1) Using remove() method

1) Using append() method 2) Using pop() method

2) Using insert() method

3) Using extend() method

Removing Elements from a List:-

You might also like