DEV Community

Guru prasanna
Guru prasanna

Posted on

Python Day-24 List Functions

extend() vs append() vs insert()-->Interview question

insert()-Adds an element at the specified position

append()-Adds single element at the end of the list.(like nested list)

extend()-Add multiple elements of a list (or any iterable), to the end of the current list.(joins elements in same list)

Example:

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

Output:

[10, 20, 30, 'abcd']
4
[10, 20, 30, 'abcd', 'p', 'q', 'r', 's']
8
Enter fullscreen mode Exit fullscreen mode

Explanation:
-->In append 'abcd' has been taken as single element and added to the end of the list.Even if a list is to be added it will be taken as single element and will join to the existing list at the end.

-->In extend 'pqrs' has been taken as different elements and added seperately to the list at the end.

reverse():
-->Reverses the order of the list.
Example:

l1 = [10,20,30]
l1.reverse()
print(l1)
Enter fullscreen mode Exit fullscreen mode

Output:

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

sort():
-->Sorts the list in Ascending order.
-->sort(reverse=True) can be used to sort in descending order.

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

l1.sort(reverse=True)
print(l1)
Enter fullscreen mode Exit fullscreen mode

Output:

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

sort vs sorted-->Interview Question
sort() modifies the original list in place and returns none, while sorted() returns a new sorted list by keeping original list unchanged.

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

l1 = [10,30,20]
l2 = sorted(l1)
print(l2)
Enter fullscreen mode Exit fullscreen mode

Output:

[10, 20, 30]
[10, 20, 30]
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.

Example:

l1 = [10,20,30,40,50]

print(min(l1))
print(max(l1))
print(sum(l1))
Enter fullscreen mode Exit fullscreen mode

Output:

10
50
150
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.

#Second Minimum value
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

Output:

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

Output:

40
Enter fullscreen mode Exit fullscreen mode

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

Image description

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

Output:

[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]
#10 20  30  40

#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

Output:

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

Example:

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

Output:

True
False
False
True
Enter fullscreen mode Exit fullscreen mode

Reversing a string
Example:

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

Output:

yadsruht si yadot
Enter fullscreen mode Exit fullscreen mode

Top comments (0)