0% found this document useful (0 votes)
6 views3 pages

Notes On List in Python

The document provides an overview of lists in Python, including how to create, access, add, and remove elements. It explains list indexing, both positive and negative, and describes methods such as append(), insert(), extend(), remove(), and pop() for manipulating lists. Additionally, it mentions functions like sort() and len() that are commonly used with lists.

Uploaded by

gulatigeetanshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Notes On List in Python

The document provides an overview of lists in Python, including how to create, access, add, and remove elements. It explains list indexing, both positive and negative, and describes methods such as append(), insert(), extend(), remove(), and pop() for manipulating lists. Additionally, it mentions functions like sort() and len() that are commonly used with lists.

Uploaded by

gulatigeetanshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LISTS IN PYTHON (pg no.

321)
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"]
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.

How to access elements of a list?


A list is made up of various elements which need to be individually accessed on the basis of the application it is
used for. There are two ways to access an individual element of a list:
a) List Index
b) Negative Indexing

a) List index: It 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. For example:
language = ['p','y','t','h','o','n']
print(my_list[0]) -------------------------------→ will give the output as p
print(my_list[4]) ]) -------------------------------→ will give the output as e
b) 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. For example:
day = ['f','r','i','d','a','y']
print(a[-1]) -------------------------------→ will give the output as y
print(a[-6]) -------------------------------→ will give the output as f

Adding Element to a List


We can add an element to any list using three methods:
1) Using append() method – adds only one element at the end of the list. For example:
L= [3,4,5]
[Link](1)
print(L)-------------------------------→ will give the output as [3, 4, 5, 1]
2) Using insert() method – adds the element at the desired position in the list. For example:
List = [1,2,3,4]
[Link](3, 12)
print(List) --------------------------→ will give the output as [1, 2, 3, 12, 4]
3) Using extend() method – adds multiple elements at the end of the list.
List = [1,2,3,4]
[Link]([8, 'Artificial', 'Intelligence'])
print(List) --------------------------→ will give the output as [1, 2, 3, 4, 8, 'Artificial', 'Intelligence']

Removing Elements from a List


Elements from a list can removed using two methods:
1) Using remove() method- It removes the specified item from the list.
List = [1, 2, 3, 4, 5, 6]
[Link](5) -----------------------→ will remove 5 from the list
print(List) --------------------------→ will give the output as [1, 2, 3, 4, 6]
2) Using pop() method- It is used to remove an element from a list at a specified index. If no index is provided,
it will remove the last element by default.
Example 1
List = [1, 2, 3, 4, 5, 6]
[Link]() -----------------------→will remove the last element i.e. 6 from the list
print(List) --------------------------→ will give the output as [1, 2, 3, 4, 5]
Example 2
List = [1, 2, 3, 4, 5, 6]
[Link](2) -----------------------→will remove element i.e. 3 from the list as 3’s index value is 2
print(List) --------------------------→ will give the output as [1, 2, 4, 5, 6]

Some more functions used with list:


a) Sort() : sorts the items of a list in ascending order.
b) len() : returns the length of a list.

You might also like