Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Python List

Dr. Harish D. Gadade


Lists in Python
● What is List? ● Python List Methods

● Memory Representation of List

● Create Python List

● Accessing List Elements in Python

● List Slicing in Python

● Change / Modify List Elements

● Delete List Elements


2
Prof. Harish D. Gadade, COEP Technological University, Pune
Create Python List
● In Python, a list is created by placing elements inside square
brackets [], separated by commas.
○ Simple List
a=[“Vihaan”,”Ahaan”, “Rihaan”,”Sohaan”]
○ Empty List a=[]
○ List with Mixed Data Types
a=[“Vihaan”,”Ahaan”, 10,2.5]
○ Nested List
a=[“Vihaan”,”Ahaan”,[10,20,30,40],”Rihaan”]

3
Prof. Harish D. Gadade, COEP Technological University, Pune
Accessing List Elements
● There are two ways of accessing list elements in python
○ List Index
○ Negative Index

4
Prof. Harish D. Gadade, COEP Technological University, Pune
Accessing List Elements
● List Index

a=[“Vihaan”,”Ahaan”,”Rihaan”,”Ayaan”]
print(a[0])
print(a[3])
print(a[1:3])
print(a[:])
print(a[:3]
print(a[1:]

a=[“Vihaan”,”Ahaan”,[10,20,30,40],”Rihaan”]
print(a[2][1])
5
Prof. Harish D. Gadade, COEP Technological University, Pune
Accessing List Elements
● Negative Index

a=[“Vihaan”,”Ahaan”,”Rihaan”,”Ayaan”]
print(a[-1])
print(a[-3])
print(a[-3:-1]

6
Prof. Harish D. Gadade, COEP Technological University, Pune
List Slicing

a = [“Ahaan”, “Vihaan”, 10, 5.2,”Rihaan”]

print(a[2:3])
print(a[:-2])
print(a[:])

7
Prof. Harish D. Gadade, COEP Technological University, Pune
Change/Modify Lists Elements

a = [“Ahaan”, “Vihaan”, 10, 5.2,”Rihaan”]

a[0]=100
print(a)

a[2:3]=[200,300]
print(a)

8
Prof. Harish D. Gadade, COEP Technological University, Pune
Add/Change Lists Elements
● We can add one item to the end of list using append() method or
can add several items using extend() method

● We can also use + operator to combine two lists. This is also


called concatenation. The * operator repeats a list for the given
number of times.
a=[1,2,3,4] a=[1,2,3,4]
a.append(5) b=[5,6,7]
print(a) print(a+b)
a.extend([10,20])
print(a) print([“coep”]*3)
9
Prof. Harish D. Gadade, COEP Technological University, Pune
Add/Change Lists Elements
● Furthermore, we can insert one item at a desired location by using the
method insert()

a=[1,2,3,4]
a.insert(1,20)
print(a)

10
Prof. Harish D. Gadade, COEP Technological University, Pune
Deleting Lists Elements
● We can use remove() to remove the given item or pop() to remove an
item at the given index.
● The pop() method removes and returns the last item if the index is not
provided. This helps us implement lists as stacks (first in, last out
data structure).
a=[10,20,30,40,50]
a.remove(20)
print(a)
a.pop(2)
print(a)
a.pop()
print(a)
11
Prof. Harish D. Gadade, COEP Technological University, Pune
Python List Methods
● Python has many useful list methods that makes it really easy to work
with lists. Here are some of the commonly used list methods.
append() adds an element to the end of the list

extend() adds all elements of a list to another list

insert() inserts an item at the defined index

remove() removes an item from the list

pop() returns and removes an element at the given index

sort() sort items in a list in ascending order


12
reverse() reverse the order of items in the list
Prof. Harish D. Gadade, COEP Technological University, Pune
List() Constructor
● It is also possible to use the list() constructor when creating a new
list.

a=list((“Vihaan”,”Ahaan”,10,20))
print(a)

Output:
[“Vihaan”,”Ahaan”,10,20]

13
Prof. Harish D. Gadade, COEP Technological University, Pune
Assignments
1. Write a python program to find maximum element from the list/array
elements
2. Write a python program to find second maximum elements from list/array
elements
3. Write a program to reverse the list / array elements
4. Write a program to search substring from a given list/array

14
Prof. Harish D. Gadade, COEP Technological University, Pune

You might also like