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

LIST

PART I
LIST
A list in python represents a set of comma separated values of any data type
between square brackets. They are mutable datas
Eg: [1,2,3,4] ,[‘a’,’x’,’y’],[23,67.4,’t’]
If a=[1,2,3] , print(a) will give the o/p as [1,2,3]
Python list are containers used for storing a list of values of any data type.
CREATING LIST
A list can be created by enclosing the values in square brackets. [ ] is used to indicate
the beginning and end of a list.
1. Empty list :- A empty list is [ ] . We can create an empty list in 2 ways
i. L= [ ] ii. L=list( )
2. Normal list:- A normal list can be created by enclosing the values in square
brackets
Eg: a=[1,2,’a’,’b’]
3. Nested list:-A list can have elements in it which itself is a list
L=[1, 2, [3,4],5]
Creating a list from an existing sequence
A list can be created from an existing sequence in the following way
L1=‘STRING’
L2=list(L1)
print(L2)
O/P➔ [‘S’, ‘T’,’R’,’I’,’N’ ,’G’]
L=list(‘python123’)
print(L)
O/P➔[‘p’,’y’,’t’,’h’,’o’,’n’,’1’,’2’,’3’]
ACCESSING ITEMS IN A LIST
We can access the list items by referring to index numbers. Index of the first item
starts from 0
Negative index is also possible which starts from -1

0 1 2 3 4 5

S T R I N G

-6 -5 -4 -3 -2 -1

Range of Indexes
We can specify a range of indexes by specifying where to start and end the range.
When the range is specified , the return value will be a new list with the specified
items from the starting index to the end index-1 \
C=[‘Red’,’Blue’,’Yellow’, ‘Green’, ‘Black’, ‘White’]
print(C[2:4])➔[‘Yellow’,’Green’]
print(C[1:5])➔[’Blue’,’Yellow’, ‘Green’, ‘Black’]
print(C[:4]➔[‘Red’,’Blue’,”yellow’,’Green’]
print(C[2:])➔[’Yellow’, ‘Green’, ‘Black’, ‘White’]

0 1 2 3 4 5

Red Green Yellow Green Black White

-6 -5 -4 -3 -2 -1

Traversing a list
Traversing a list means accessing each element of a list. This can de done either
using for or while loop statement. It can be done in 2 ways:
O/P:
1. Using in operator inside for loop
p
Eg: L=[‘p’,’y’,’t’,’h’,’o’,’n’]
y
for i in L:
print(i) t
h
O
n

L=[‘p’,’y’,’t’,’h’,’o’,’n’]
O/P:
for i in L:
[‘p’,’y’,’t’,’h’,’o’,’n’]
print(L)
[‘p’,’y’,’t’,’h’,’o’,’n’]
[‘p’,’y’,’t’,’h’,’o’,’n’]
[‘p’,’y’,’t’,’h’,’o’,’n’]
[‘p’,’y’,’t’,’h’,’o’,’n’]
[‘p’,’y’,’t’,’h’,’o’,’n’]
2. Using range function
Eg: L=[1,2,3,4,5]
O/P:
n=len (L) 1
for i in range(n): 2
print(i)
3
4
5
L=[‘p’,’y’,’t’,’h’,’o’,’n’]
O/P:
n=len(L)
p
for i in range(n):
print(L[i]) y
t
h
o
n

Comparing lists
Python allows to compare 2 lists. Each element is individually compared in
lexicographical order. All relational operators are used for comparing 2 lists if they
are of comparable type. Otherwise it reports an error

Comparison Result Reason

[1,2,3,4] <[4,5,6] True 1 < 4 is True

[1,2,3,4] < [1,2,5,3] True 3<5 is True

[1,2,3] > [2,4,6] False 1>2 is False

[‘a’,’b’,’c’] < [‘d’,’e’,’f’] True a< d is True

[‘A’,’B’,’C’] > [‘a’,’b’,’c’] False ‘A’ > ‘a’ is False


65 > 97 is False

Eg: L1=[4,5,6,7,8,9,10]
L2=[4,5,6,7]
print (L1<L2)
O/P:
print (L2<L1)
False
print (L1>L2)
print (L2>L1) True
L3=[4,5,[6,7],8,9,10]
print(L1 <L3)
True
False
Operations on a list
Python provides basic operations that can be performed on a list. They are
1. Concatenation(+)
2. Replication(*)
3. Membership Testing- in /not in
4. Slicing
1.Concatenation(+) : It is the process in which multiple list can be joined with the
help of operators. Python uses + operator for joining the list
Eg:
L1=[1,2,3] O/P:
L2=[4,5,6]
[1,2,3,4,5,6]
L3=L1+L3
print(L3) [4,5,6,1,2,3]
L2=L2+L1
print(L2) [1,2,3,4,5,6,1,2,3]
L1=L1+L2
[4,5,6,1,2,3]
print(L1)
print(L2) [1,2,3,4,5,6]
print(L3)
2. Replication(*):- It replicates the list for a specified number of times.
Eg: L=[10,20,30]
O/P:
print(L*2)
A=[1,5,8]
[10,20,30,10,20,30]
A= A* 3
print(A)
[1,5,8,1,5,8,1,5,8]
B=[‘a’,’b’,’c’]
c=L+B
print (c *2)
[10,20,30,’a’,’b’,’c’,10,20,30,’a’,’b’,’c’]

3. Membership Testing:- It is an operation used to check whether a particular


element is a member of the list or not. The membership testing is done using the in
operator. It returns true if the element is in the list, otherwise it returns false
Eg: L=[1,2,3,4,5]
print(5 in L)
O/P:
print(6 in L) True
print( 2 not in L)
print(8 in L)
False
False
False
4. Slicing:-Slicing is done with the help of index value. Slicing is an operation in
which you can slice a particular range from the list. List slices are the subpart of a
list
Syntax: List[start:stop:step]
Start:- It is the starting index
Stop: It is the ending index, but this index is not included
Step: It is the step value
Eg:
L=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
print(L[1:4])
print(L[1:6:2])
print(L[3:])
0 1 2 3 4 5 6 7
print(L[:5])
c o m p u t e r
print(L[-1])
print(L[-3:]) -8 -7 -6 -5 -4 -3 -2 -1
print(L[2:7:2])
print(L[-1:-7:-1])
print(L[-8:-2:2])

O/P:
[‘o’,’m’,’p’]
[‘o’, ’p’, ’t’]
[‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘c’, ‘o’ , ‘m’, ‘p’, ‘u’]
[‘r’]
[‘t’, ‘e’, ‘r’]
[‘m’, ‘u’, ‘e’]
[‘r’, ‘e’, ‘t’, ‘u’, ‘p’, ‘m’]
[‘c’, ‘m’, ‘u’]
Qn: Find the output
L=[‘P’,’Y’,’T’,’H’,’O’,’N’,’ ‘,’P’,’R’,’O’,’G’,’R’,’A’,’ M’]
print(L[1:6])
print(L[-6:-2])
print(L[2:10:2])
print(L[-2:-11:-1])
print(L[-5:-8])
print(L[:7])
print(L[ : : ])
print(L[ : : -1])
print(L[4:])
print(L[-7:-1])
print(L[-4:-12:-2])

0 1 2 3 4 5 6 7 8 9 10 11 12 13

P Y T H O N P R O G R A M

-14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1


ANS:

['Y', 'T', 'H', 'O', 'N']

['R', 'O', 'G', 'R']

['T', 'O', ' ', 'R']

['A', 'R', 'G', 'O', 'R', 'P', ' ', 'N', 'O']

[]

['P', 'Y', 'T', 'H', 'O', 'N', ' ']

['P', 'Y', 'T', 'H', 'O', 'N', ' ', 'P', 'R', 'O', 'G', 'R', 'A', 'M']

['M', 'A', 'R', 'G', 'O', 'R', 'P', ' ', 'N', 'O', 'H', 'T', 'Y', 'P']

['O', 'N', ' ', 'P', 'R', 'O', 'G', 'R', 'A', 'M']

['P', 'R', 'O', 'G', 'R', 'A']

You might also like