L6 Lists
L6 Lists
LIST MANIPULATION
Introduction of list sequence
datatype
• Python lists are containers that are used to store a list of values of any
type.
• Python lists are mutable means the element of a list can be modified.
• So python will not create a new list when changes are made in lists.
NOTE: mutable means memory location will not change even if list’s
elements are edited i.e. values can change]
Creating list
• Lists are created using [ ] square brackets.
• Lists can store a sequence of values of any type or mixed
datatypes. Lists data is separated by comma(,)
• EGs
L1=[] #this is empty list
L1=[1,2,3] # list of integers
L1=[‘a’, ‘b’, ‘c’] #list of strings
L1=[5.5,4.4,3,3] #list of floats
L1=[1, ‘Superman’, 99.4] #mixed list
Creating list
• EG1: empty list is []. The empty list’s truth value will be
false. Creating an empty list:
L1=[]
print(L1)
L2=list()
print(L2)
print(L1==True) o/p False
B’coz truth value of L1 is False
• Long lists
L1=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
Creating list
• EG2: Long lists- A list can contain many elements i.e. no
limit.
L1=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
Creating and accessing list
• EG3: Nested lists- A list can have an elements in it, which
itself is a list.
PROGRAM1:
L1=[1, ‘Ram’, [30,40,50], ‘aundh’]
L1[0] is 1 L1[1] is ‘Ram’
L1[2] is [30,40,50] L1[3] is ‘aundh’
PROGRAM2:
L=[30,40,50]
Stud1=[1, ‘Ram’, L, ‘aundh’]
#Here L is another list and part of list Stud1
Creating lists from existing sequence
• Built-in list type object is used to create lists from
sequence. Sequence object can be of any kind i.e. string,
tuples or lists itself.
• If another list is pass as an object a copy of the list is
formed.
• Syntax: L1=list(<sequence>) #list() is function.
Here, “hello” is string sequence converted to list.
Here y variable is of int type and z’s value is inputting from user and also of int type
Use [] square brackets for lists type data and () round brackets for tuple type data.
Comma is used as separator.
Accessing list
• List is compound datatype.
• List data can be change i.e. mutable (means can add/delete/modify
element(s). Accessing list is somewhat similar to string.
• List data is separated by comma(,)
• List data is represented in square brackets [ ].
• Eg1 a=[1, 2, 3, 4, 5]
b=[‘a’, ‘e’, ‘I’, ‘o’, ‘u’]
c=[‘Donna’, 102, 87.5]
0 1 2 {each item has index no. ,starts with 0}
Indexes in Lists
• Lists also have index no of each element i.e. forward index nos and backward
index nos.
Accessing elements of List
Understanding more on lists
len() function returns the no. of elements in list
#Program2
L=['a1','p2','p3','l4','e5']
for a in L:
print(a,end=“ “)
Using range( ) function
• Index elements can be accessed using range( ) and len( ) function.
#Program
L=['a','p','p','l','e']
le=len(L)
print(le)
for a in range(le):
print("at index",a, 'and',(a-le),'element:',L[a])
Comparing Lists (>, <, ==, !=)
• Python internally compares individual elements of lists and tuples in
lexicographical order.
• Means each corresponding element is compared to another list’s
element. For EG:
L1>L3 o/p False as 1 <4 compares 1st element of L1 with 1st element of L2
L1,L2=[1,2,3],[‘1’,’2’,’3’]
L1==L2 o/p False
#string and int comparison always results to false
L1>L2 o/p ERROR cannot compare sting & int
List operations
• Joining lists i.e. + concatenation operator: will join 2 or more lists.
L1,L2,L3=[1,2],[10,20],[100,200]
LL3=L1+L2+L3
print(LL3) o/p [1,2,10,20,100,200]
L4=[‘a’, ‘b’]
L5=L1+L4
print(L5) o/p [1,2,’a’,’b’]
NOTE: for + operator both operands must be of list type else gives an error.
EG. L1+’a’
L1+10 ERROR
L1+complex no
• L2=L2+’abc’ gives error bcoz += is different from + for mutable iterable
types such as lists. Both operators have different rules in python.
• += is used with lists on the right side to iterable and it will add each
element of the iterable to the list.
• L2+=‘abc’ does not expand to L2 =L2+’abc’ for all types if L2 is list.
• Remember L2+=1 will give an ERROR as int is not iterable.
i=10
i+=1 OR i=i+1 11
Different memory location
L1=[1,3]
L1+=‘abc’ L1 [1,3, ‘a’, ‘b’,’c’]
Yet they r in same memory
L1=L1+’abc’ #ERROR
List operations
• Replication operator (*) : this operator is used to replicate a list specified
number of times.
L1=[1,2]
print(L1*3) o/p [1,2,1,2,1,2]
L4=[‘a’, ‘b’]
print(2*L4) o/p [‘a’,’b’,’a’,’b’]
-9 -8 -7 -6 -5 -4 -3 -2 -1
[10,12,14,20,22,24,30,32,34]
0 1 2 3 4 5 6 7 8
Seq [20,22,24]
Seq[0] ??20
Find output:
Lst=[2,4,6,8,10,12,14,16,18,20] Write a code to Print 18 to 10 ?
Lst[3:8]
Lst[-2:-7:-1]
Lst[1:8:1]
Lst[2:8:2] Lst[-2:3:-1]
Lst[0:10:3]
Lst[8:3:-1]
Lst[-8:-3]
Lst[-3:-8]
Lst[-3:-8:-1]
Lst[-3:3] ??
Lst[-3:3:1]
Lst[-3:3:-1]
Lst[0:11:3]
Lst[::4]
Lst[::-1]
Lst[::-3]
Lst=[2,4,6,8,10,12,14,16,18,20]
• Program 6.2 Page 190
Using slice for list modification
EG1:
EG2:
• It gave error b’coz 55 is integer and not a iterable object
Note:
if list created using list() then the
new list remain unchanged and also
refer to different memory location.
Means both lists work independently.
print(lst)
print(lst2)
Using in operator
lst=[17,24,15,30,27]
lst2=lst.copy()
for no in lst2:
lst2=no+10
print(lst2,end=" ")
print(lst)
List functions and methods
• Every list object in python is actually an instance of List class. Various
operations can be performed on lists like appending (adding
items/elements), updating or deleting etc.
• Function can have fun_name, return type, parameter(s)/ argument(s)
• Function Prototype:
int sum(int,int..)
• Syntax :
<List_Object_name>.<method_name>()
Lets study the all functions in detailed:
1. len() function
• Returns the length of its argument list, i.e. no. of elements in list.
• Syntax: len(<List_Object>)
EG:
• If index is less than zero and not equal to any of the valid negative
indexes of the list, then adds the value in the beginning of the list.
• Similarly if index is positive and more then the length the adds the
value at last. Remember will NOT give an error
7. pop() function
• Removes/deletes the single item from the list. Takes one optional
argument and returns the deleted value.
• Syntax:<list_object>.pop(<index_No>)
Will delete the item as specified index.
OR
<list_object>.pop()
Will delete the last item if index is not specified.
Returns the deleted item
pop() takes only one argument
else gives an error.
• NOTE: the pop() takes index no. as argument and removes the item of
specified index, by returning the deleted item/element.
• Whereas, remove() take value/item as argument, removes the first
matching element and does not return any thing.
9. clear() function
• This method removes all items from the list; means list becomes empty
after this function. This function returns nothing.
• EG
del L[:]
Will make the list empty will remove
all elements from the list
del L[::1]
del L[::]
Deleting List object
• del <List_object> OR del(<List_object>)
Will delete the list object from the memory
NOTE: del and del() works same
Descending order
• EG2: sorting tuple EG:4 sorting str