Home > Backend Development > Python Tutorial > day-at payilagam [Lists]

day-at payilagam [Lists]

DDD
Release: 2025-01-04 21:08:41
Original
942 people have browsed it

day-at payilagam [Lists]

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]

Copy after login
  • 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 
Copy after login

in for loop:

for data in student_data:
    print(data)

Copy after login
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

Copy after login

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]

Copy after login

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]

Copy after login

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]

Copy after login

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
Copy after login

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

Copy after login

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]
[]

Copy after login

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

Copy after login

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 

Copy after login

The above is the detailed content of day-at payilagam [Lists]. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template