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

PART – 1

LIST
List is used to store a collection / sequence of values of any type.

It is mutable.
We can change the elements of a list at a specified place.

To create a list, put the elements in square brackets:

Syntax: ListName = [value1, value2, ….., valueN]


myList = [ ]  Creates an empty list.

myList = list()  Creates a empty list with constructor.

myList = [2, 9, 22]  Creates a list with integers.

myList = [‘this’, ‘is’, ‘test’]  Creates a list with string values.

myList = [12, ‘this’, 3.6, ‘a’]  Creates a list with mixed values.

Nested List:
myList = [5, 7, ['this', 'is'], 50]

6/19/2020 Pyari Mohan Sahu - 7008146305 2


A= 4
La = [10, 5, 44, 88]
0 1 2 3

print (La[2])
Length: it is equals to number of elements in the
list
Forward Backward
Positive Negative
Starting: 0 negative length
End: length minus 1 -1
-3 -2 -1
Li = [10, 20, 30]
0 1 2
Mutable immutable
[] ()

A list can be created using constructor


such as: myL = List()

A list can be created using constructor


such as: myL = Tuple()

An element of list can be changed.


An element of tuple can not be changed.
LIST - Create
listOne = [10, 20, 30] eval() used to evaluate and return the
result of an expression given as string.
listTwo = tuple(listOne)
myList = eval(input("Enter 3 marks: "))
Accept a list from the user: Enter 3 marks: [10, 20, 30]
myList = list(input(“Enter numbers: ”)) print(myList)
Enter numbers: 1256 [10, 20, 30]
print(myList)
['1', '2', '5', '6'] eval('10 + 20')
30
myList = list(input(“Enter numbers: ”))
Enter numbers: 12 56 var = eval(input('Enter data: '))
print(myList) Enter data: 20
['1', '2', ‘ ‘, '5', '6'] print( var, type(var) )
20 <class 'int'>
myList = list(input("Enter numbers: "))
Enter numbers: [10, 20, 30] var = eval(input('Enter data: '))
print(myList) Enter data: [10, 20]
['[', '1', '0', ',', ' ', '2', '0', ',', ' ', '3', '0', ']'] print(var, type(var) )
[10, 20] <class 'list'>

6/19/2020 Pyari Mohan Sahu - 7008146305 6


LIST
Like string it also provides forward and backward indexing.

We can access an element with the help of index.

More than one element can be accessed with the help of slicing.

Like string we can also use membership operators: in and not in.

Like string we can also use:


Concatenate operator (+) Replication operator ( * )

myList = [‘t’, ‘h’, ‘I’, ‘s’, ‘i’, ‘s’, ‘d’, ‘e’, ‘m’, ‘o’]
Question
>>> print(myList [1])  h
1) var = eval(input('Enter data: '))
>>>print(myList [22])  Index Error Enter data: [10, ‘a’]
print (type (var) )
>>> print(myList [2 : 5])  ['I', 's', 'i']

>>>print(myList[-8 : -2])  ['I', 's', 'i', 's', 'd', 'e'] Output: ?

>>>print(myList[-9 : -3 : 2])  ['h', 's', 's']


6/19/2020 Pyari Mohan Sahu - 7008146305 7
LIST - Display
myList = ['t', 'h', 'i', 's', 'i', 's', 'd', 'e', 'm', 'o']

To display the contents of the list:


print(myList)  ['t', 'h', 'I', 's', 'i', 's', 'd', 'e', 'm', 'o']

To display one element at a time:


for c in range(len(myList)):
print(myList[c], end = " ") Output
Index Positive: 0 Negative: -10 Data: t
Output: t h i s i s d e m o Index Positive: 1 Negative: -9 Data: h
Index Positive: 2 Negative: -8 Data: i
for ch in myList: Index Positive: 3 Negative: -7 Data: s
print(ch, end = ", ") Index Positive: 4 Negative: -6 Data: i
Index Positive: 5 Negative: -5 Data: s
Output: t, h, i, s, i, s, d, e, m, o, Index Positive: 6 Negative: -4 Data: d
Index Positive: 7 Negative: -3 Data: e
To display index with data: Index Positive: 8 Negative: -2 Data: m
length = len(myList) Index Positive: 9 Negative: -1 Data: o
for c in range(length):
print('Index Positive: ', c, 'Negative: ', (c - length), 'Data: ', myList[c]

6/19/2020 Pyari Mohan Sahu - 7008146305 8


LIST Assignment
myList = ['t', 'h', 'i', 's', 'i', 's', 'd', 'e', 'm', 'o']
print(myList[ : : -1])
Output:
['o', 'm', 'e', 'd', 's', 'i', 's', 'i', 'h', 't']

Question Check
myList[3] = ‘Q’
2) print(myList[3 : -10 : -1])
print(myList)
3) print(myList[3 : 10 : -1]) Output: ['t', 'h', 'i', 'Q', 'i', 's', 'd', 'e', 'm', 'o']

myList[0 : 2] = 'This', 'is', 'demo'


4) print(myList[3 : 20 : 2]) print(myList)
Output: ['This', 'is', 'demo', 'i', 's', 'i', 's', 'd', 'e', 'm', 'o']
5) print(myList[20])
myList[0 : 3] = 'This', 'is', 'demo'
6) print(myList[-3 : -9 : -2]) print(myList)
Output: ['This', 'is', 'demo', 's', 'i', 's', 'd', 'e', 'm', 'o']
7) print(myList[-9 : -7])
myList[0 : 7] = 'Demo'
8) print(myList[ : : -3]) print(myList)
Output: ['D', 'e', 'm', 'o', 'e', 'm', 'o']
6/19/2020 Pyari Mohan Sahu - 7008146305 9
LIST Slicing
9) myList = [10, 20, 30]
myList [1 : 2] = “502”
print(myList)
Output: ?

Output Write code

myList = [1, 2, ['this', 'is'], 3] Write code


myList = [1, 2, 3]
myList [ : 5] = [33, 44] myList = [1, 2, [ [2.5, 3.2], 'T', 'A'], 3]
13) To display: 2
10) print(myList) 19) To display: 2.5
14) To display: this
myList = [1, 2, 3] 20) To display: T
myList [ : 5] = 33, 44 15) To display: this 3
11) print(myList) 21) To display: 3, 2.5
16) To display: 2 this
myList = [1, 2, 3] 22) To display: A [2.5 3.2]
myList [ : 5] = 33 17) To display: this is
12) print(myList) Write down the output
18) To display: is this
23) print(myList[2])

6/19/2020 Pyari Mohan Sahu - 7008146305 10


Nested LIST
myList = [ 'Ram', [10, 20, 30], 'Amit', [50,60,70], 'Binod', [60,65,70] ]
print(myList[0], myList[1][0], myList[1][1], myList[1][2])

Output: Ram 10 20 30

Using for loop: Using for loop: Using for loop:


(value based) (Index based) (Index based)

for name in myList: for c in range ( len(myList) ): for c in range(0, len(myList), 2):
print(name) print(myList[c]) print(myList[c], myList[c + 1])

Output: Output: Output:


Ram Ram Ram [10, 20, 30]
[10, 20, 30] [10, 20, 30] Amit [50, 60, 70]
Amit Amit Binod [50, 60, 70]
[50, 60, 70] [50, 60, 70]
Binod Binod
[60, 65, 70] [60, 65, 70]

6/19/2020 Pyari Mohan Sahu - 7008146305 11


LIST - Comparison
Each individual elements of list (and tuple) compared lexicographical order.

To check equal, each corresponding element must compare equal and two
sequences must be of same type.
Example
first, second = [9, 8, 7], [9, 8, 7] 30) print(first > second)  False
third, forth = [9, 4, 8], [ [9, 8], 7]
31) print(first > third)  True
24) print(first == second)  True
32) print(third > first)  False
25) print(first == third)  False
33) print([1, 2, 5] > [1, 10, 9, 1])  False
26) print(first == forth)  False
34) print([1, 2, 5] < [1, 10, 9, 1])  True
27) print(first < second)  False
35) print([1, 2] == [1.0, 2.0])  True
28) print(first < third)  False
36) print([1, 2] == ['1', '2']) ?
29) print(first < forth)  Error
not supported between instances of 37) print([1, 2] > ['1', '2']) ?
'int' and 'list'

6/19/2020 Pyari Mohan Sahu - 7008146305 12


LIST - Functions
1) append()
To add an element at the end of the list.

Syntax: listObject . append (data) Questions

Example 40) first = [9, 8, 7]


38) first = [9, 8, 7] first.append(10, 12)
first.append(10) print("First: ", first)
print(first)
41) first = [9, 8, 7]
Output: [9, 8, 7, 10] second = [55, 40, roomNo5]
second.append(first)
39) first = [9, 8, 7] first.append(10)
second = first.append(10)
print("First: ", first, "\nSecond: ", second)
print("First: ", first, "\nSecond: ",second)

Output:
First: [9, 8, 7, 10]
Second: None

6/19/2020 Pyari Mohan Sahu - 7008146305 13


LIST - Functions
2) del()
To delete an individual element, or delete all elements identified by slice.
It retuns nothing.
Questions
Syntax: 45) first = [19, 20, 21, 22, 23]
del listObject[<index>] del first[1 : 4]
del listObject[ <start> : <stop> : <step> ] print(first)

Example 46) first = [19, 20, 21, 22, 23]


42) first = [10, 20] del first[1 : ]
del first print(first)
print(first)
Output: Error: name 'first' is not defined 47) first = [19, 20, 21, 22, 23]
del first[ : 2]
43) first = [19, 20, 21, 22, 23] print(first)
del first[1]
print(first) 48) first = [19, 20, 21, 22, 23]
Output: [19, 21, 22, 23] del first[ : 12]
44) first = [19, 20, 21, 22, 23] print(first)
del first[1:30]
print(first) 49) first = [19, 20, 21, 22, 23]
Output: [19] del first[1 : 12 : 2]
print(first)
6/19/2020 Pyari Mohan Sahu - 7008146305 14
LIST - Functions
3) pop()
To delete an individual element and returns it.
Cannot delete list slice.
Without index positon will delete the last element.

Syntax:
listObject.pop([index])

Example
50) first = [19, 20, 21, 22, 23]
first.pop()
print(first)
Output: [19, 20, 21, 22]

51) first = [19, 20, 21, 22, 23]


print(first.pop(3))
Output: 22

52) first = [19, 20, 21, 22, 23]


print(first.pop(30))
Output: Error: pop index out of range

6/19/2020 Pyari Mohan Sahu - 7008146305 15


LIST - Functions
4) len()
This function will return number of element in the list.
Syntax: len(listObject)
Returns integer value.

Example:
53) first = [22, 33, 44, 55, 66]
print(len(first))  5

5) index()
This function will the index of the first matched item from the list.
Syntax: listObject.index(<item>)
Returns integer value.

Example:
54) first = [19, 20, 21, 20, 23]
print(first.index(20))  1

55) first = [19, 20, 21, 20, 23]


print(first.index(90))  ValueError: 90 is not in list

6/19/2020 Pyari Mohan Sahu - 7008146305 16


LIST - Functions
6) count()
This function returns the count of the item passed as argument.
Syntax: listObject.count(<item>)
Returns integer value.

Example:
57) first = [22, 33, 22, 55, 22]
56) first = [22, 33, 22, 55, 22]
print(first.count(90))  0
print(first.count(22))  3

7) reverse()
Reverse the item of the list.
It does not returns anything.
Syntax: listObject.reverse()

Example:
58) first = [22, 33, 44, 55, 66] 59) first = [22, 33, 44, 55, 66]
first.reverse() second = first.reverse()
print(first)  [66, 55, 44, 33, 22] print(first, second) ?

6/19/2020 Pyari Mohan Sahu - 7008146305 17


LIST - Functions
8) extend()
This function is used to add multiple elements to a list.
Syntax: listObject.extend(<list>)
The parameter list is added at the end of the list object.
Returns nothing.

Examples:
Question
60) first = [10, 11, 12]
62) first = [10, 11, 12]
second = ['t', 'h', 'i']
second = first.extend(['T', 'H'])
first.extend(second)
print("First: ", first, "Second: ", second)
print("First: ", first, “\nSecond: ", second)
Output:
First: [10, 11, 12, 't', 'h', 'i']
63) first = [10, 11, 12]
Second: ['t', 'h', 'i']
second = first.extend('T', 'H')
print("First: ", first, "Second: ", second)
61) first = [10, 11, 12]
second = first.extend(['T', 'H'])
64) first = [10, 11, 12]
print("First: ", first, "Second: ", second)
second = "This is demo"
Output:
first.extend(second[1:3])
First: [10, 11, 12, 'T', 'H'] Second: None
print(first)

6/19/2020 Pyari Mohan Sahu - 7008146305 18


LIST - Functions
9) insert()
This function is used to add an elements at given position to a list.
Syntax: listObject.insert(<position>, <item>)
It takes two argument:
Position: It is the index position before which item to be instered.
If the index is greater than the length of the list then item is appended.
Item: Data to insert.
Returns nothing. Question
Examples: 67) first = ["I", "am", "mohan"]
65) first = [10, 20, 30, 40] first.insert(-3, 'Hello')
first.insert(2, 25) print(first)
print(first)
Output: 68) Write a insert statement to insert at
[10, 20, 25, 30, 40] the beginning of the list.

66) first = [10, 20, 30, 40] 69) Write a insert statement to insert at
first.insert(25, 2) the end of the list.
print(first)
Output: 70) first.append(10)
[10, 20, 30, 40, 2] Write an equivalent insert function for
the above statement.

6/19/2020 Pyari Mohan Sahu - 7008146305 19


LIST - Functions
10) remove()
This function removes the first occurrence of given item from the list.
Syntax: listObject.remove(<item>)
Item: Data to delete.
Returns nothing.
If item is not available in the list will display error message.

Question
Examples: 73) first = [1, 2, 3, 1]
71) first = [1, 2, 3, 1] first.remove(1, 2)
first.remove(1) print(first)
print(first)
Output: 74) first = [10, 20, 30, 40]
[2, 3, 1] second = [10, 20]
first.remove(second[1])
72) first = [1, 2, 3, 1] print(first)
first.remove(10)
print(first)
Output:
Error: list.remove(x): x not in list

6/19/2020 Pyari Mohan Sahu - 7008146305 20


LIST - Functions
11) clear()
This function removes the the items from the list and list becomes empty.
Syntax: listObject.clear()
Returns nothing.

12) sort()
This function sorts the items of the list.
Default is ascending order.
If the parameter is reverse = True then sort it in descending order.
Does not return anything.
sort()
clear() 76) first = [10, 2, 30, 37, 5, 45]
Examples: first.sort()
75) first = [10, 2, 30, 37, 5, 45] print(first)
first.clear() Output: [2, 5, 10, 30, 37, 45]
print(first)
Output: 77) first = [10, 2, 30, 37, 5, 45]
[] first.sort(reverse = True)
print(first)
Output: [45, 37, 30, 10, 5, 2]

6/19/2020 Pyari Mohan Sahu - 7008146305 21


LIST - Functions
13) max()
This function returns the maximum element from the list.
Syntax: max(listObject)

14) min()
This function returns the minimum element from the list.
Syntax:min(listObject)

min()
max()
Examples:
Examples:
80) li = [44,11,22,33,11,10]
78) li = [44,11,22,33,11]
print(“Minimum: ", min(li))
print("Maximum: ", max(li))
Output:
Output:
Maximum: 10
Maximum: 44
Question
Question
81) l = [44,11,22,33,10]
79) l = [44,11,22,33,11]
print(“Minimum: ", min(l[1:3]))
print("Maximum: ", max(l[2 : ]))
Output: ????
Output: ????

6/19/2020 Pyari Mohan Sahu - 7008146305 22


Difference
Difference between del and pop
del() pop()
Can remove list slice Cannot remove list slice
Cannot return deleted element Returns deleted element
It does not takes index position as It takes index position as parameter
parameter
del with list object can delete the Cannot delete the object.
complete list and object too.

Difference between append and extend


append() extend()
Add an element at the end. Adds an argument list at the end.
Adds one element. Adds alls the elements of the list.
Increase the length of the list by 1. Increase the length of the list by the
length of the inserted list.

6/19/2020 Pyari Mohan Sahu - 7008146305 23


Difference
Difference between pop and remove
pop() remove()
Removes element at given index. Removes an element.
Returns the deleted element. Does not returns the deleted element.
Takes index as parameter. Takes value as parameter

Difference between del and clear


del() clear()
Deletes the list itself. Deletes all the element of the list.
Can delete give slice elements. Removes all.
Takes parameter. Does not takes parameter.

6/19/2020 Pyari Mohan Sahu - 7008146305 24

You might also like