Professional Documents
Culture Documents
XI IP List Manipulation 30-08-2023
XI IP List Manipulation 30-08-2023
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
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]]
L=list(<sequence>)
Example:1
l1=list("hello")
print(l1)
Output
Example:2
t=('h','e')
l1=list(t)
print(l1)
Output
['h', 'e']
for i in <list>:
process each item
eg:
for i in [1,2,3]:
print(i)
Output:
1
2
3
⮚ LIST OPERATION
● Concatenation (+)
● Replication(*)
⮚ 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:
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:
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:
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)
append()
extend()
pop()
BIRLA PUBLIC SCHOOL, DOHA-QATAR
INFORMATICS PRACTICES
LIST MANIPULATION
GRADE: 11 30.08-2023
sort()
index()
insert()
remove()
reverse()
clear()
count()
*********************************************