LIST (Class XI)
•It is
an ordered set of values enclosed in square brackets [].
•Values
in the list can be modified, i.e. it is mutable.
•As it
is set of values, we can use index in square
brackets [] to identify a value belonging to it.
•The
values that make up a list are called its elements, and they can be of any
type.
. Its
syntax is:
Variable
name [index] (variable name is name of the list).
Let's look
at some example of simple list:
i)
>>>L1 = [1, 2, 3, 4] # list of 4 integer elements.
ii)
>>>L2 = [“Delhi”, “Chennai”, “Mumbai”] #list of 3 string elements.
iii)
>>>L3 = [ ] # empty list i.e. list with no element
iv)
>>>L4 = [“abc”,
10, 20] # list with different types of elements
v)
>>>L5 = [1, 2, [6, 7, 8], 3] # A list containing another list known as
nested
list
Creating and Accessing List
•To
create a list, put a number of expressions in square brackets.
L=[]
L=[value1,
value2………]
•The
empty list
The
empty list is []. It is equivalent to 0 or ‘ ‘.
For
example
L=list()
It
will generate an empty list and name that L
•Long
list
If a
list contains many elements, then to enter such long list, you can split it
across several lines.
•Creating
list from existing sequences
Syntax:
L=list(<sequence>)
>>>l1=list(‘hello’)
>>>l1
[‘h’,
’e’, ’l’, ’l’, ’o’]
>>>a=“amit”
>>>l2=list(a)
•Creating list by input from keyboard during run time
For
Example:
(1) To store data as String:
>>>
l1=list(input("Enter elements"))
Enter
elements12345
>>>
l1
['1',
'2', '3', '4', '5']
(2) To store data as integers:
>>>
l2=eval(input("Enter
elements"))
Enter
elements12345
>>>
l2
12345
>>>
a=[]
b=int(input("Enter
total elements of list::"))
for i
in range(b):
c=int(input("Enter element::"))
a.append(c)
print(a)
Accessing
List
For
accessing the elements of a list the bracket operator is used.
The
expression inside the brackets specifies the index. The indices start at 0.
For
example:
>>>
l1=[1,2,3,4,5]
>>>
l1[1]
2
>>>
print(l1[1])
2
>>>
l1[0]
1
>>>
l1[4]
5
>>>
>>>
l1=[1,2,3,4,5]
>>>
l1[-1]
5
>>>
l1[-2]
4
>>>
l1[-5]
1
>>>
l1[-6]
Traceback
(most recent call last):
File "<stdin>", line 1, in <module>
IndexError:
list index out of range
>>>
Traversing List
•Traversing
a list means, process or go through each element of the list sequentially. The
most common way to traverse the elements of a list is with a for loop.
•The
syntax is:
For<List_Item>
in <List>:
statement to process<List+Item>
OR
For<index>in
range(len(List)):
statement to process<List[Index]>
For
Example:
Using for
(1) num=[11,12,13,14,15,16]
for i
in num :
print(i,end=" ")
(2) num=[11,12,13,14,15,16]
for i
in range(0,5,1):
print(num[i],end=" ")
Using While
num=[11,12,13,14,15,16]
i=0
n=len(num)
while
i<n :
print(num[i],end=" ")
i=i+1
Print a list in reverse order
Using for loop
num=list(input("Enter
the numbers:: "))
n=len(num)
for i in
range(n-1,-1,-1):
print(num[i],end=" ")
Using while loop
num=list(input("Enter
the numbers:: "))
n=len(num)
i=n-1
while
i>=0
:
print(num[i],end=" ")
i=i-1
Lists are Mutable :-
Lists
are mutable, which means we can change their elements.
Using
the bracket operator([ ]) on the left side of an assignment, we can update one
of the elements.
For
example:
>>>
l1=[11,22,33,44,55]
>>>
l1
[11,
22, 33, 44, 55]
>>>
l1[2]=333
>>>
l1
[11,
22, 333, 44, 55]
>>>
l1[-1]=555
>>>
l1
[11,
22, 333, 44, 555]
List Operations:-
Joining List:
>>>
l1=[1,2,3]
>>>
l2=[4,5,6]
>>>
l1+l2
[1,
2, 3, 4, 5, 6]
>>>
print(l1+l2)
[1,
2, 3, 4, 5, 6]
>>>
l3=l1+l2
>>>
print(l3)
[1,
2, 3, 4, 5, 6]
The
+ operator when used with lists require that both the operands must be of list
Type.
Repeating or Replicating Lists:-
>>>
l1=[1,2,3]
>>>
l1*3
[1,
2, 3, 1, 2, 3, 1, 2, 3]
Comparing List:-
Lists
can be compared by using relational operators.
Python compares individual elements of list.
>>>a=[1,2,3]
>>>b=[1,2,3]
>>>c=[‘1’,’2’,’3’]
>>>d=[1.0,2.0,3.0]
>>>a==b
True
>>>a==c
False
>>>a>b
False
>>>a==d
True
List Slice:-
An
individual element of a list is called slice. Selecting a slice is similar to
selecting an element of a list.
L[start:stop]
creates list slice out of list L with the elements falling between start and
stop, not included stop
>>>
l1=[10,20,30,40,50,60,70]
>>>
l1
[10,
20, 30, 40, 50, 60, 70]
>>>
l1[2:5]
[30,
40, 50]
>>>
print(l1[3:6])
[40,
50, 60]
L[start:stop:step]
slice out L with elements falling between indexes start and stop, not including
stop, skipping step-1 elements in between.
>>>
l1=[11,22,33,44,55,66,77,88]
>>>
l1[0:9:2]
[11,
33, 55, 77]
>>>
l1[0:9:3]
[11,
44, 77]
>>>
l1[::3]
[11,
44, 77]
Create a list and print the sum and average of all the elements of list.
a=[]
sum=avg=0
b=int(input("Enter
total
elements of list::"))
for i in
range(b):
#for i in
range(0,b,1):
c=int(input("Enter element::"))
a.append(c)
for j
in range(0,b+1,1):
sum=sum+j
avg=sum/b
print(sum)
print(avg)
Slice the list and print sum & average
sum=avg=count=0
l1=[1,2,3,4,5,6,7,8,9,10]
s1=l1[1:11:2]
print(s1)
for j
in s1:
sum=sum+j
count=count+1
avg=sum/count
print(sum)
print(avg)
List Functions and Methods:-
Python
also offers many built-in functions and methods for list manipulation. Every
list object that
we create in Python is actually an instance of list class.
1.
index: List.index(<item>)
>>>L1=[12,14,16,18]
>>>L1.index(14)
>>>1
2.
append:
>>>Colors=[‘red’,
‘blue’, ‘green’]
>>>Colors.append(‘yellow’)
3.extend:
>>>a1=[‘a’,
‘e’, ‘i’]
>>>a2=[‘o’,’u’]
>>>a1.extend(a2)
>>>a1
[‘a’,
‘e’, ‘i’, ‘o’,’u’]
Difference between append() and
extend()
append()
adds one element to the list, extend() can add multiple elements from the list
supplied to it as argument.
>>>a1=[1,2,3]
>>>a1.append(6,7) # Produce error
>>>a1.append([6,7])# List can be appended but
length will increase by 1
>>>len(a1)
4
>>>a1
[1,2,3,[6,7]]
>>>a1=[1,2,3]
>>>a2=[4,5]
>>>a1.extend(a2) or
>>>a1.extend([4,5])
>>>a1
[1,2,3,4,5]
>>>len(a1)
5
After
append() the length of list will increase by 1 only, after extend() length of
list will increase by length of inserted list.
4.
insert(): list.insert(<pos>,
<item>)
>>>a1=[1,2,4,5]
>>>a1.insert(2,3)
>>>a1
[1,2,3,4,5]
Other
formats
List.insert(0,x)
List.insert(len(a1),x)
# will place element at last of list
5.pop():
It removes an element from the list.
List.pop(<index>)
>>>a1=[‘a’,
‘b’, ‘c’, ‘d’]
>>>x=a1.pop(0)
>>>x
‘a’
>>>y=a1.pop(2)
>>>y
‘d’
>>>a1
[‘b’,
‘c’]
6.remove(): It removes the first occurrence of given item from list, it takes element as an
argument(where as pop() takes index as argument)
List.remove(<value>)
>>>a1=[1,2,3,4,5,6,7]
>>>a1.remove(2)
>>>a1
[1,3,4,5,6,7]
7.
clear(): It removes all the items of the list.
List.clear()
>>>a1=[1,2,3,4,5]
>>>a1.clear()
>>>a1
[]
8.count():It
returns the count of item passed as argument.
List.count(<item>)
>>>a1.count(2)
1
9.reverse():
It reverse the items of the list.
List.reverse()
>>>a1=[1,2,3,4,5,6,7]
>>>a1.reverse()
>>>a1
[7,6,5,4,3,2,1]
10.sort():It
sort the items of list, by default in increasing order.
List.sort()
>>>a1=[2,1,6,3,8,4]
>>>a1.sort()
>>>a1
[1,2,3,4,6,8]
>>>a1.sort(reverse=True)
# for sorting in decreasing order.
append()
adds one element to the list, extend() can add multiple elements from the list
supplied to it as argument.
>>>a1=[1,2,3]
>>>a1.append(6,7) # Produce error
>>>a1.append([6,7])# List can be appended but
length will increase by 1
>>>len(a1)
4
>>>a1
[1,2,3,[6,7]]
>>>a1=[1,2,3]
>>>a2=[4,5]
>>>a1.extend(a2) or
>>>a1.extend([4,5])
>>>a1
[1,2,3,4,5]
>>>len(a1)
5
After
append() the length of list will increase by 1 only, after extend() length of
list will increase by length of inserted list.
4.
insert(): list.insert(<pos>,
<item>)
>>>a1=[1,2,4,5]
>>>a1.insert(2,3)
>>>a1
[1,2,3,4,5]
Other
formats
List.insert(0,x)
List.insert(len(a1),x)
# will place element at last of list
5.pop():
It removes an element from the list.
List.pop(<index>)
>>>a1=[‘a’,
‘b’, ‘c’, ‘d’]
>>>x=a1.pop(0)
>>>x
‘a’
>>>y=a1.pop(2)
>>>y
‘d’
>>>a1
[‘b’,
‘c’]
6.remove(): It removes the first occurrence of given item from list, it takes element as an
argument(where as pop() takes index as argument)
List.remove(<value>)
>>>a1=[1,2,3,4,5,6,7]
>>>a1.remove(2)
>>>a1
[1,3,4,5,6,7]
7.
clear(): It removes all the items of the list.
List.clear()
>>>a1=[1,2,3,4,5]
>>>a1.clear()
>>>a1
[]
8.count():It
returns the count of item passed as argument.
List.count(<item>)
>>>a1.count(2)
1
9.reverse():
It reverse the items of the list.
List.reverse()
>>>a1=[1,2,3,4,5,6,7]
>>>a1.reverse()
>>>a1
[7,6,5,4,3,2,1]
10.sort():It
sort the items of list, by default in increasing order.
List.sort()
>>>a1=[2,1,6,3,8,4]
>>>a1.sort()
>>>a1
[1,2,3,4,6,8]
>>>a1.sort(reverse=True)
# for sorting in decreasing order.
Comments
Post a Comment