DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

1

Day 23 - List and List functions

List:

List is represented by
List is a collection of heterogeneous data(Different datatype).
List is index_based
List is mutable(changeable)

Example:

student_data = ['Pritha', 'B.E', 30, True, 5.6]
print(student_data)
Enter fullscreen mode Exit fullscreen mode
['Pritha', 'B.E', 30, True, 5.6]
Enter fullscreen mode Exit fullscreen mode

Example using while loop:

student_data = ['Pritha', 'B.E', 30, True, 5.6]
i = 0 
while i<len(student_data):
    print(student_data[i],end=' ')
    i+=1

Enter fullscreen mode Exit fullscreen mode
Pritha B.E 30 True 5.6
Enter fullscreen mode Exit fullscreen mode

Example using for loop:

student_data = ['Pritha', 'B.E', 30, True, 5.6]
for data in student_data:
    print(data, end=" ")
Enter fullscreen mode Exit fullscreen mode
Pritha B.E 30 True 5.6
Enter fullscreen mode Exit fullscreen mode

enumerate():

It is used to create the index in the loop.
Also used for grouping and index tracking.

student_data = ['Pritha', 'B.E', 30, True, 5.6]
index = 0
for index,data in enumerate(student_data):
    print(index, data)
    index+=1

Enter fullscreen mode Exit fullscreen mode
0 Pritha
1 B.E
2 30
3 True
4 5.6

Enter fullscreen mode Exit fullscreen mode

List is mutable so we can change any element from the list.

student_data = ['Pritha', 'B.E', 30, True, 5.6]
print(student_data)

student_data[1] = 'M.E'
print(student_data)
Enter fullscreen mode Exit fullscreen mode
['Pritha', 'B.E', 30, True, 5.6]
['Pritha', 'M.E', 30, True, 5.6]

Enter fullscreen mode Exit fullscreen mode

append():

It is used to add an element to the end of a list.
It modifies the list in-place and does not return a new list.

Create a list using append():

employee = []
employee.append('Raja')
employee.append('Madurai')
employee.append('B.Sc.,')
employee.append(5.2)
employee.append(True)

print(employee)
Enter fullscreen mode Exit fullscreen mode
['Raja', 'Madurai', 'B.Sc.,', 5.2, True]

Enter fullscreen mode Exit fullscreen mode

insert():

It is used to insert an element at a specific position in a list.

employee = ['Raja', 'Madurai', 'B.Sc.,', 5.2, True]
employee.insert(2, 'Tamil Nadu')
print(employee)

Enter fullscreen mode Exit fullscreen mode
['Raja', 'Madurai', 'Tamil Nadu', 'B.Sc.,', 5.2, True]
Enter fullscreen mode Exit fullscreen mode

remove():

It is used to remove the first occurrence of a specific element from a list.
If the element is found, it is removed, and the list is modified in place.
If the element is not present in the list, a ValueError is raised.

employee = ['Raja', 'Madurai', 'B.Sc', 5.2, True]
employee.remove('Madurai')
print(employee)

Enter fullscreen mode Exit fullscreen mode
['Raja', 'B.Sc', 5.2, True]
Enter fullscreen mode Exit fullscreen mode

pop():

It is used to remove and return an element from a list at a specific index.

employee = ['Raja', 'Madurai', 'B.Sc', 5.2, True]
employee.pop(3) 
print(employee)

Enter fullscreen mode Exit fullscreen mode
['Raja', 'Madurai', 'B.Sc', True]
Enter fullscreen mode Exit fullscreen mode

del statement:

It is used to delete an element from a list by index.
It does not return a value; it simply removes the item or object from memory.

l = [10,20,30,40,50,60]
del l[2]
print(l)
Enter fullscreen mode Exit fullscreen mode
[10, 20, 40, 50, 60]
Enter fullscreen mode Exit fullscreen mode

We can use del to remove an item from a list by specifying a slice.

l = [10,20,30,40,50,60]
del l[2:4]
print(l)

Enter fullscreen mode Exit fullscreen mode
[10, 20, 50, 60]
Enter fullscreen mode Exit fullscreen mode

Difference between pop() and del:

l = [10,20,30,40,50,60]
del l[:]
print(l)
l = [10,20,30,40,50,60]
print(l.pop())

Enter fullscreen mode Exit fullscreen mode
[]
60
Enter fullscreen mode Exit fullscreen mode

del[:]- Delete everything from the list
pop()- Delete last element from the list.

Write a program to calculate total mark and percentage:

marks_list = [90,97,97,65,78]
total = 0
l=len(marks_list)
for mark in marks_list:
    total+=mark 
print(total)

percentage=total/l
print("percentage:",percentage)

Enter fullscreen mode Exit fullscreen mode
427
percentage: 85.4

Enter fullscreen mode Exit fullscreen mode

Write a Program to find the highest mark from the given mark:

highest = 0
marks_list = [90,97,96,65,98]

for mark in marks_list:
    if mark>highest:
        highest = mark #90 97 

print(highest)

Enter fullscreen mode Exit fullscreen mode
98
Enter fullscreen mode Exit fullscreen mode

Write a program to find the lowest mark from the given mark:

lowest = 100
marks_list = [90,97,96,65,98]

for mark in marks_list:
    if mark<lowest:
        lowest = mark 

print(lowest)
Enter fullscreen mode Exit fullscreen mode
65
Enter fullscreen mode Exit fullscreen mode
scores = [90,167, 208,45,32]
lowest = scores[0]

for score in scores:
    if score<lowest:
        lowest = score 

print(lowest)

Enter fullscreen mode Exit fullscreen mode
32
Enter fullscreen mode Exit fullscreen mode

isinstance()-used to check if an object is an instance of a specified class or any of its subclasses.
It returns True if the object matches the specified class or type, otherwise it returns False.

Write a program to find str datatype:

data_list = ['abcd','pqrs','xyz',1234, 1.234,True]
for data in data_list:
    if isinstance(data,str):
        print(data)

Enter fullscreen mode Exit fullscreen mode
abcd
pqrs
xyz

Enter fullscreen mode Exit fullscreen mode

Write a program to find str datatype and make them upper:

data_list = ['abcd','pqrs','xyz',1234, 1.234,True]
for data in data_list:
    if isinstance(data,str):
        print(data.upper())
Enter fullscreen mode Exit fullscreen mode
ABCD
PQRS
XYZ

Enter fullscreen mode Exit fullscreen mode

Write a program to find str datatype and make them upper for first two letter:

data_list = ['abcd','pqrs','xyz','a','m',1234, 1.234,True]
for data in data_list:
    if isinstance(data,str):
        if len(data)>= 2:
            print(data.upper()[:2])
Enter fullscreen mode Exit fullscreen mode
AB
PQ
XY

Enter fullscreen mode Exit fullscreen mode

Task:
1) contains n --> names
2) names have 5 letters
3) t --> names end with

names_list = ['sachin','dhoni','rohit','virat']
for name in names_list:
    if 'n' in name:
        print(name,end=" ")
print()
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=" ")
Enter fullscreen mode Exit fullscreen mode
sachin dhoni 
dhoni rohit virat 
rohit virat 
Enter fullscreen mode Exit fullscreen mode

# SaChIn DhOnI rOhIt vIrAt

names_list = ['sachin', 'dhoni', 'rohit', 'virat']

for index, name in enumerate(names_list):
    players = []

    if index < 2:

        for i in range(len(name)):
            if i % 2 == 0:
                players.append(name[i].upper())
            else:
                players.append(name[i].lower())
    else:

        for i in range(len(name)):
            if i % 2 != 0:
                players.append(name[i].upper())
            else:
                players.append(name[i].lower())

    print(''.join(players), end=' ')
Enter fullscreen mode Exit fullscreen mode
SaChIn DhOnI rOhIt vIrAt
Enter fullscreen mode Exit fullscreen mode
👋 While you are here

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay