Home >Backend Development >Python Tutorial >Python Day-List Functions

Python Day-List Functions

DDD
DDDOriginal
2024-12-25 06:27:48742browse

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))

Output:

[10, 20, 30, 'abcd']
4
[10, 20, 30, 'abcd', 'p', 'q', 'r', 's']
8

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)

Output:

[30, 20, 10]

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)

Output:

[10, 30, 200]
[200, 30, 10]

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)

Output:

[10, 20, 30]
[10, 20, 30]

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))

Output:

10
50
150

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)

Output:

20

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)

Output:

40

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

Python Day-List Functions

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)

Output:

[30, 20, 10, 40]
[20, 10, 30, 40]

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

Output:

[10, 20, 30, 40]
20

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)

Output:

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

Reversing a string
Example:

[10, 20, 30, 'abcd']
4
[10, 20, 30, 'abcd', 'p', 'q', 'r', 's']
8

Output:

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

The above is the detailed content of Python Day-List Functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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