Professional Documents
Culture Documents
List Theory
List Theory
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.
myList = [12, ‘this’, 3.6, ‘a’] Creates a list with mixed values.
Nested List:
myList = [5, 7, ['this', 'is'], 50]
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
[] ()
More than one element can be accessed with the help of slicing.
Like string we can also use membership operators: in and not in.
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']
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']
Output: Ram 10 20 30
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])
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'
Output:
First: [9, 8, 7, 10]
Second: None
Syntax:
listObject.pop([index])
Example
50) first = [19, 20, 21, 22, 23]
first.pop()
print(first)
Output: [19, 20, 21, 22]
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
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) ?
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)
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.
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
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]
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: ????