DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

Day 24 - List Functions

extend() vs append() vs insert():

extend()-Adds all elements from an iterable (e.g., list, tuple) to the end of the list.
append()-Adds a single element to the end of the list.
insert()-Inserts a single element at a specified index in the list.

Example for extend():

l1 = [10,20,30]
l2 = [40,50,60]
l1.extend(l2)
print(l1)
print(len(l1))

Enter fullscreen mode Exit fullscreen mode
[10, 20, 30, 40, 50, 60]
6
Enter fullscreen mode Exit fullscreen mode

Example for append():

l1 = [10,20,30]
l2 = [40,50,60]
l1.append(l2)
print(l1)
print(len(l1))

Enter fullscreen mode Exit fullscreen mode
[10, 20, 30, [40, 50, 60]]
4
Enter fullscreen mode Exit fullscreen mode

append() vs extend():

l1 = [10,20,30]
l1.append('abcd')
print(l1)
l1.extend('pqrs')
print(l1)
Enter fullscreen mode Exit fullscreen mode
[10, 20, 30, 'abcd']
[10, 20, 30, 'abcd', 'p', 'q', 'r', 's']

Enter fullscreen mode Exit fullscreen mode

reverse():

The reverse() method in Python is used to reverse the order of the elements in a list in place.

l1 = [10,20,30]
l1.reverse()
print(l1)

Enter fullscreen mode Exit fullscreen mode
[30, 20, 10]
Enter fullscreen mode Exit fullscreen mode

sort():

The sort() method in Python is used to sort the elements of a list in place. It arranges the elements in ascending order by default.

l1 = [10,200,30]
l1.sort()
print(l1)

l1.sort(reverse=True)
print(l1)

Enter fullscreen mode Exit fullscreen mode
[10, 30, 200]
[200, 30, 10]

Enter fullscreen mode Exit fullscreen mode

sorted():

The sorted() function in Python is similar to the sort() method, but with key differences. It returns a new sorted list without modifying the original list.

my_list = [3, 1, 4, 5, 2]
sorted_list = sorted(my_list)
print(sorted_list) 
print(my_list) 
Enter fullscreen mode Exit fullscreen mode
[1, 2, 3, 4, 5]
[3, 1, 4, 5, 2]
Enter fullscreen mode Exit fullscreen mode

min(): To find minimum value in given input.
max(): To find maximum value in given input.
sum(): To find sum of all values in given input.

l1 = [10,200,30,40,50]
print(min(l1))
print(max(l1))
print(sum(l1))

Enter fullscreen mode Exit fullscreen mode
10
200
330
Enter fullscreen mode Exit fullscreen mode

inf(Infinity):

float('inf'):Used to find maximum number.
-float('inf'):Used to find minimum number.

1. Find second minimum value in given input.

l1 = [10,20,310,40,50]
min_value = float('inf')  
second_min = float('inf')  

i = 0
while i <len(l1):
    if l1[i]<min_value: 
        second_min = min_value
        min_value = l1[i] 
    elif l1[i]<second_min:
        second_min = l1[i]
    i+=1
else:
    print(second_min)
Enter fullscreen mode Exit fullscreen mode
20
Enter fullscreen mode Exit fullscreen mode

2. Find second maximum value in given input.

l1 = [10,20,30,40,50]
max_value = -float('inf')  
second_max = -float('inf') 

i = 0
while i <len(l1):
    if l1[i]>max_value: 
        second_max = max_value
        max_value = l1[i] 
    elif l1[i]>second_max:
        second_max = l1[i]
    i+=1
else:
    print(second_max)

Enter fullscreen mode Exit fullscreen mode
40

Enter fullscreen mode Exit fullscreen mode

Bubble sort:

It compares adjacent elements, and swaps them if they are in the wrong order.

Example:1

l1 = [40,30,20,10]
i = 0 
while i<len(l1)-1:
    if l1[i]>l1[i+1]:
        l1[i], l1[i+1] = l1[i+1], l1[i]
    i+=1
print(l1)
i = 0 
while i<len(l1)-2:
    if l1[i]>l1[i+1]:
        l1[i], l1[i+1] = l1[i+1], l1[i]
    i+=1
print(l1)

Enter fullscreen mode Exit fullscreen mode
[30, 20, 10, 40]
[20, 10, 30, 40]

Enter fullscreen mode Exit fullscreen mode

Example:2(using double sort change given list in ascending order and find Kth highest value in a given list)

l1 = [40,30,20,10]

#kth highest value in a given list
j = 1
while j<=len(l1):
    i = 0 
    while i<len(l1)-j:
        if l1[i]>l1[i+1]:
            l1[i], l1[i+1] = l1[i+1], l1[i]
        i+=1
    j+=1
print(l1)
print(l1[-3])
Enter fullscreen mode Exit fullscreen mode

[10, 20, 30, 40]
20

Enter fullscreen mode Exit fullscreen mode

in and not in operators:

Python’s in and not in operators allow you to quickly determine if a given value is or isn’t part of a collection of values.

l1 = [100,67,54,101,220, 670,45, 32]
print(100 in l1)
print(100 not in l1)
print(120 in l1)
print(120 not in l1)
Enter fullscreen mode Exit fullscreen mode
True
False
False
True
Enter fullscreen mode Exit fullscreen mode

Reversing a string:

s = "today is thursday"
reverse = ""
i = 0

while i<len(s):
    reverse = reverse + s[-(i+1)]
    i+=1

print(reverse)

Enter fullscreen mode Exit fullscreen mode
yadsruht si yadot
Enter fullscreen mode Exit fullscreen mode

Top comments (0)