what is list:
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks) that is used to store an ordered collection of items. We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe stored at different locations.
here list is mutable and string is immutable
student_data = ['Guru Prasanna', 'B.Com', 21, True, 5.6]
print(student_data)
output:
['Guru Prasanna', 'B.Com', 21, True, 5.6]
- list is a collection of data
- list has heterogeneous(various)data
- list is index based
student_data = ['Guru Prasanna', 'B.Com', 21, True, 5.6]
i = 0
while i<len(student_data):
print(student_data[i],end=' ')
i+=1
output:
Guru Prasanna B.Com 21 True 5.6
in for loop:
for data in student_data:
print(data)
student_data = ['Guru Prasanna', 'B.Com', 21, True, 5.6]
index=0
for index,data in enumerate(student_data):
print(index,data)
index+=1
output:
0 Guru Prasanna
1 B.Com
2 21
3 True
4 5.6
enumerate method index the values.
student_data = ['Guru Prasanna', 'B.Com', 21, True, 5.6]
print(student_data)
student_data[1] = 'M.Com'
print(student_data)
output:
['Guru Prasanna', 'B.Com', 21, True, 5.6]
['Guru Prasanna', 'M.Com', 21, True, 5.6]
just removes and adds the new position value what we have just coded. we have to mention which positional value.
employee = []
employee.append('Raja')
employee.append('Madurai')
employee.append('B.Sc.,')
employee.append(5.2)
employee.append(True)
print(employee)
output:
['Raja', 'Madurai', 'B.Sc.,', 5.2, True]
It is called as empty list.By,append method it
appends the value of the list.
employee.insert(2,'Tamilnadu')
print(employee)
employee.remove('Madurai')
print(employee)
employee.pop(3)
print(employee)
output:
['Raja', 'Madurai', 'Tamilnadu', 'B.Sc.,', 5.2, True]
['Raja', 'Tamilnadu', 'B.Sc.,', 5.2, True]
['Raja', 'Tamilnadu', 'B.Sc.,', True]
['Raja', 'Tamilnadu', 'B.Sc.,', True]
In this case,append means adding at the end and insert means inbetween the values as we are giving specific index to fit the value.
- remove(value based removal)
- pop(index based removal)
l = [10,20,30,40,50,60]
del l[2]
print(l)
output:
[10, 20, 40, 50, 60]
l = [10,20,30,40,50,60]
del l[2:4]
print(l)
output:
[10, 20, 50, 60]
l = [10,20,30,40,50,60]
print(l.pop(2))
output:
30
in first case del is a keyword,
in second case we are using indexing
in third case we are taking out second index and printing it out.
l = [10,20,30,40,50,60]
print(l)
print(l.pop(2))
print(l)
print(l.pop())
print(l)
print(l.pop(123))
output:
[10, 20, 30, 40, 50, 60]
30
[10, 20, 40, 50, 60]
60
[10, 20, 40, 50]
Traceback (most recent call last):
File "/home/main.py", line 15, in <module>
print(l.pop(123))
IndexError: pop index out of range
in this case, pop(2) takes the index value and prints the value.
pop() prints the value of the last.
pop(123) prints error. there is no value assigned to it.
l = [10,20,30,40,50,60]
print(l)
del l[:] # del l
print(l)
output:
[10, 20, 30, 40, 50, 60]
[]
after adding del keyword without any indexing inside it deletes everything and gives an empty list.
data_list = ['abcd','pqrs','xyz',1234, 1.234,True]
for data in data_list:
if isinstance(data,str):
print(data.upper())
output:
ABCD
PQRS
XYZ
task:
names_list = ['sachin','dhoni','rohit','virat']
for name in names_list:
if len(name)==5:
print(name,end=' ')
print()
for name in names_list:
if name[-1] == 't':
print(name,end=' ')
print()
Output:
dhoni rohit virat
rohit virat
Top comments (0)