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

Python Day-List Functions

DDD
Release: 2024-12-25 06:27:48
Original
790 people have browsed it

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

Output:

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

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

Output:

[30, 20, 10]
Copy after login

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

Output:

[10, 30, 200]
[200, 30, 10]
Copy after login

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

Output:

[10, 20, 30]
[10, 20, 30]
Copy after login

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

Output:

10
50
150
Copy after login

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

Output:

20
Copy after login

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

Output:

40
Copy after login

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

Output:

[30, 20, 10, 40]
[20, 10, 30, 40]
Copy after login

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

Output:

[10, 20, 30, 40]
20
Copy after login

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

Output:

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

Reversing a string
Example:

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

Output:

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

The above is the detailed content of Python Day-List Functions. 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