Class 13 List Methods
Class 13 List Methods
1 Slicing
• Fetching a sublist from a list is known as slicing.
[3]: # lst[start_index:stop_index:step_size]
# Stop_index is excluded
# Step_size is 1 by default
['Python', True, {1: 100, 2: 200}, None, 50, (1, 2, 3), (10+20j), False, 5.5]
<class 'list'>
[5.5, False, (10+20j), (1, 2, 3), 50, None, {1: 100, 2: 200}, True, 'Python']
[5.5, False, (10+20j), (1, 2, 3), 50, None, {1: 100, 2: 200}, True, 'Python']
[9]: -len(lst)-1
[9]: -10
1
3 When the step is -ive
• Start is -1 by default.
• Stop is -len(lst)-1 by default.
[12]: lst
[12]: ['Python', True, {1: 100, 2: 200}, None, 50, (1, 2, 3), (10+20j), False, 5.5]
[20]: len(lst)
[20]: 9
[38]: print(lst[0:9])
print(lst[0:len(lst):1])
print(lst[::])
['Python', True, {1: 100, 2: 200}, None, 50, (1, 2, 3), (10+20j), False, 5.5]
['Python', True, {1: 100, 2: 200}, None, 50, (1, 2, 3), (10+20j), False, 5.5]
['Python', True, {1: 100, 2: 200}, None, 50, (1, 2, 3), (10+20j), False, 5.5]
[27]: # lst[9]
print(lst[-1:-len(lst)-1:-1])
print(lst[::-1])
[5.5, False, (10+20j), (1, 2, 3), 50, None, {1: 100, 2: 200}, True, 'Python']
[5.5, False, (10+20j), (1, 2, 3), 50, None, {1: 100, 2: 200}, True, 'Python']
[40]: lst[:5000]
[40]: ['Python', True, {1: 100, 2: 200}, None, 50, (1, 2, 3), (10+20j), False, 5.5]
[42]: # lst[50]
[43]: print(lst[0:5:-1])
[]
[44]: lst
[44]: ['Python', True, {1: 100, 2: 200}, None, 50, (1, 2, 3), (10+20j), False, 5.5]
2
[50]: # [5.5,(1,2,3),{1:100,2:200}]
# [(10+20j),50,{1:100,2:200},'Python']
[53]: print(lst[::-3])
print(lst[-3::-2])
5 Case_1
• When we already know the elements
6 Case_2
• Using list()
[56]: # list(iterable)g
# list() takes an iterable as an argument and returns a list
[62]: my_str='Python'
print(my_str,type(my_str))
print(len(my_str))
print()
lst=list(my_str)
print(lst,type(lst))
print(len(lst))
[66]: t=(10,20,30,40,50)
print(t,type(t))
lst=list(t)
3
print(lst,type(lst))
[71]: d={1:100,2:200,3:300,4:400}
print(d,type(d))
lst=list(d)
print(lst,type(lst))
lst=list(d.values())
print(lst,type(lst))
lst=list(s)
print(lst,type(lst))
[76]: f=frozenset({10,20,30,40,50,60})
print(f,type(f))
lst=list(f)
print(lst,type(lst))
[82]: r=range(1,5)
print(r,type(r))
lst=list(r)
print(lst,type(lst))
lst=list(range(5))
print(lst,type(lst))
4
7 Case_3
• By taking the input from the user
[85]: list('123')
[87]: ['H', 'e', 'l', 'l', 'o', ' ', 'P', 'y', 't', 'h', 'o', 'n']
8 eval()
• It evaluates the type of the value.
5
[94]: a=eval('10')
print(a,type(a))
10 <class 'int'>
[95]: a=eval('2.5')
print(a,type(a))
[99]: a=eval('"Hello"')
print(a,type(a))
[97]: "Hello"
[97]: 'Hello'
[101]: abc=10
abc
[101]: 10
[103]: a=eval('[10,20,30,40]')
print(a,type(a))
[105]: input()
55
[105]: '5'
[106]: eval('5')
[106]: 5
[109]: eval('Hello')
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_15708\3378726434.py in <module>
----> 1 eval('Hello')
6
<string> in <module>
[112]: lst=[]
for i in range(5):
a=eval(input('Enter a value- '))
lst=lst+[a]
print(lst)
Enter a value- 10
Enter a value- 2.5
Enter a value- True
Enter a value- "Python"
Enter a value- 10+20j
[10, 2.5, True, 'Python', (10+20j)]
[113]: lst=[]
for i in range(5):
a=eval(input('Enter a value- '))
lst=lst+[a] # or lst.append(a)
print(lst)
Enter a value- 40
Enter a value- 50
Enter a value- "Hello"
Enter a value- [1,2,3,4]
Enter a value- (10,20,30)
[40, 50, 'Hello', [1, 2, 3, 4], (10, 20, 30)]
[114]: lst
40 <class 'int'>
50 <class 'int'>
Hello <class 'str'>
[1, 2, 3, 4] <class 'list'>
(10, 20, 30) <class 'tuple'>
[117]: lst=[]
for i in range(5):
a=input('Enter a value- ')
lst=lst+[a] # or lst.append(a)
7
print(lst)
Enter a value- 10
Enter a value- 20
Enter a value- Hello
Enter a value- (1,2,3,4,5)
Enter a value-
['10', '20', 'Hello', '(1,2,3,4,5)', '']
10 <class 'str'>
20 <class 'str'>
Hello <class 'str'>
(1,2,3,4,5) <class 'str'>
<class 'str'>
9 Case_4
• By using list comprehension
• List comprehension is the easier way to create a list using loop
P
y
t
h
o
n
8
lst=[]
for i in range(2,21,2):
lst+=[i] # lst=lst+[i]
print(lst)
# print(lst)
[141]: lst=[]
lst+=[2]
print(lst)
lst+=[4]
print(lst)
lst+=[6]
print(lst)
[2]
[2, 4]
[2, 4, 6]
[154]: print(ord('A'))
print(ord('Z'))
65
90
[159]: print(chr(65))
print(chr(66))
print(chr(89))
print(chr(90))
A
B
Y
Z
65 A
9
66 B
67 C
68 D
69 E
70 F
71 G
72 H
73 I
74 J
75 K
76 L
77 M
78 N
79 O
80 P
81 Q
82 R
83 S
84 T
85 U
86 V
87 W
88 X
89 Y
90 Z
[166]: lst=[]
for i in range(65,91):
lst+=[chr(i)]
print(lst)
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
10
[178]: lst=[i**2 for i in range(1,11,1)]
print(lst)
[181]: # Create a list of the numbers that are divisible by 2 but not divisible by 5␣
↪from 1 to 50
lst=[]
for i in range(1,51):
if i%2==0 and i%5!=0:
# print(i)
lst+=[i]
print(lst)
[2, 4, 6, 8, 12, 14, 16, 18, 22, 24, 26, 28, 32, 34, 36, 38, 42, 44, 46, 48]
[2, 4, 6, 8, 12, 14, 16, 18, 22, 24, 26, 28, 32, 34, 36, 38, 42, 44, 46, 48]
[184]: # Create a list that has the squares of the even numbers and the cubes of the␣
↪odd numbers using list comprehension
# Output- [1,4,,27,16,125,36,343,64,729,100]
[192]: lst=[]
for i in range(1,11):
if i%2==0:
print(i**2)
lst+=[i**2]
else:
print(i**3)
lst+=[i**3]
print(lst)
1
4
27
16
125
36
343
64
729
100
[1, 4, 27, 16, 125, 36, 343, 64, 729, 100]
11
[193]: # [ternary operator for loop]
[195]: i=3
i**2 if i%2==0 else i**3
[195]: 27
Enter a value- 10
Enter a value- 20
Enter a value- 'Helo'
Enter a value- 2.5
Enter a value- (1,2,3,4,5)
[10, 20, 'Helo', 2.5, (1, 2, 3, 4, 5)]
[201]: [1, 4, 27, 16, 125, 36, 343, 64, 729, 100]
10 List methods
[203]: lst=[10,20,30,40,50]
print(dir(lst))
12
11 append()
• It adds the element at the end of the list
[205]: # lst.append(value)
[212]: lst=[10,20,30]
print(lst)
lst.append(100)
print(lst)
lst.append('Python')
print(lst)
lst.append((1,2,3,4,5))
print(lst)
lst.append(0.25)
print(lst)
lst.append({2,5})
print(lst)
[213]: lst=[]
for i in range(1,11):
if i%2==0:
# print(i**2)
lst.append(i**2)
else:
# print(i**3)
lst.append(i**3)
print(lst)
12 extend()
• It takes an iterable as an argument.
• It adds the elements of the iterable to the list one by one
13
[214]: # lst.extend(iterable)
[216]: lst=[10,20,30]
print(lst)
lst.extend('Python')
print(lst)
[217]: lst=[10,20,30]
print(lst)
lst.append('Python')
print(lst)
[218]: lst=[10,20,30]
print(lst)
lst.append((5,5,5))
print(lst)
[219]: lst=[10,20,30]
print(lst)
lst.extend((5,5,5))
print(lst)
[227]: lst=[10,20,30]
lst+={1:100,2:200}.values()
print(lst)
[231]: lst=[10,20,30]
lst=lst+['Python']
lst
14
[231]: [10, 20, 30, 'Python']
[237]: lst=[]
lst=lst+['Hello']
print(lst)
['Hello']
[238]: lst=[]
lst+='Hello'
print(lst)
[239]: a = 'Python'
b = ''
#Apply logical opereators (and, or & not) on above string values and observe␣
↪the output.
print(a and b)
print(b and a)
print(a or b)
print(b or a)
print(not a)
print(not b)
Python
Python
False
True
[241]: bool('Python'),bool('')
[243]: a and b
[243]: ''
[245]: bool('a'),bool('b')
15
[246]: True and True
[246]: True
[247]: 'b'
[249]: False
[252]: 0+0j
[252]: 0j
print(str1<=str2)
print(str1>=str2)
print(str1==str2)
print(str1!=str2)
True
True
True
False
print(str1>=str2)
print(str1<=str2)
print(str1==str2)
print(str1!=str2)
16
True
False
False
True
[ ]: my_str = "Although 8 that way may not be obvious at first unless you're Dutch"
#Write the code to get the total count of 't' in above string. Use find() and␣
↪index() method.
17