You are on page 1of 6

BIRLA PUBLIC SCHOOL, DOHA-QATAR

INFORMATICS PRACTICES
LIST MANIPULATION
GRADE: 11 30.08-2023

INTRODUCTION

Python Lists are mutable or modifiable (can change the elements of a list in
place) Python will not create a fresh list when you make changes to an
element of a list. In other words, memory address of a list will not change
even after the value is changed

It contains values of mixed data types.

⮚ CREATING & ACCESSING LIST

List is a standard data type of Python that can store a sequence of values
belonging to any type. Lists are depicted through square brackets

[ ] empty list
[1, 2, 3] list of integers
[1, 2, 3, 2.5] list of numbers
[‘a’, ’b’, 1, 1.5] list of mixed data types
[‘a’, ’b’] list of characters
[‘one’, ’two’] list of strings

⮚ CREATING LIST

● Empty list
L=list[]

● Long List

L=[12,3,34,5,56,6,7,7,8,3,4,5,9,12,3,34,5,56,6,7,7,8,3,4,5]

● Nested List
BIRLA PUBLIC SCHOOL, DOHA-QATAR
INFORMATICS PRACTICES
LIST MANIPULATION
GRADE: 11 30.08-2023

L=[3,4,5,6,[1,2,3]]

⮚ CREATING LISTS FROM EXISTING SEQUENCES

L=list(<sequence>)

where sequence object includes strings, tuples, lists

Example:1

l1=list("hello")
print(l1)
Output

['h', 'e', 'l', 'l', 'o']

Example:2

t=('h','e')
l1=list(t)
print(l1)

Output

['h', 'e']

⮚ Differences from string


Strings are not mutable
Lists are mutable

Both are sequences /core data types


BIRLA PUBLIC SCHOOL, DOHA-QATAR
INFORMATICS PRACTICES
LIST MANIPULATION
GRADE: 11 30.08-2023

⮚ TRAVERSING A LIST means accessing or processing each element


(traversing using for loop)

for i in <list>:
process each item

eg:

for i in [1,2,3]:
print(i)
Output:
1
2
3

⮚ LIST OPERATION

● Length len() returns the number of items in the list

● Concatenation (+)

● Replication(*)

● Membership operators in , not in

● Indexing and Slicing

l[i] returns item at index i


l[i:j] returns items all items at indexes between i and j excluding the
index j

⮚ SLICING-.
List slices: sub part of the list is extracted
BIRLA PUBLIC SCHOOL, DOHA-QATAR
INFORMATICS PRACTICES
LIST MANIPULATION
GRADE: 11 30.08-2023

Seq= L[start:stop]

Example 1

lst=[10,20,30,40,50,60,70,80,90,100]
seq=lst[3:-3] # from index 3 to <-3
print(seq)

Output:

[40, 50, 60, 70]

Example 2
lst=[10,20,30,40,50,60,70,80,90,100]
print(lst[3:-3]) # from index 3 to <-3
print(lst[1]) # element at index 1
print(lst[20:30]) # index out of bounds – empty list
print(lst[-20:-1]) # start index is not available , hence start from index 0, to <
-1
print(lst[9:11]) # from index 9 to <11

Output:

[40, 50, 60, 70]


20
[]
[10, 20, 30, 40, 50, 60, 70, 80, 90]
[100]

List also support slice steps


Seq=L[start:stop:step value]

Example
lst=[10,20,30,40,50,60,70,80,90,100]
BIRLA PUBLIC SCHOOL, DOHA-QATAR
INFORMATICS PRACTICES
LIST MANIPULATION
GRADE: 11 30.08-2023
print(lst[0:6:2]) # from index 0 to <6 with step 2
print(lst[::2]) # from index 0 to end , with step 2
print(lst[5::2]) # index 5 to end with step 2
print(lst[::-1]) # reverse the list

Output:

[10, 30, 50]


[10, 30, 50, 70, 90]
[60, 80, 100]
[100, 90, 80, 70, 60, 50, 40, 30, 20, 10]

⮚ WORKING WITH LISTS-if statement is used to execute statements


based on condition.

Appending:append() method adds a single item to the end of the list


List.append(item)

Updating
To update or change an element of the list in place,assign new value
element
list[index]= <new value>

Deleting
Remove items from lists.

del list[index]
del list[<start>:<stop>]

list.pop(index)

⮚ LIST FUNCTIONS AND METHODS


Syntax:
<List object>.<methodname>

append()
extend()
pop()
BIRLA PUBLIC SCHOOL, DOHA-QATAR
INFORMATICS PRACTICES
LIST MANIPULATION
GRADE: 11 30.08-2023
sort()
index()
insert()
remove()
reverse()
clear()
count()

*********************************************

You might also like