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

Day - List Functions

Linda Hamilton
Linda HamiltonOriginal
2025-01-03 11:51:39909browse

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

[10, 20, 30, 40, 50, 60]
6

Example for append():

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

[10, 20, 30, [40, 50, 60]]
4

append() vs extend():

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

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)

[30, 20, 10]

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)

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

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) 
[1, 2, 3, 4, 5]
[3, 1, 4, 5, 2]

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

10
200
330

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

40

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)

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

#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])
[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.

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

Reversing a string:

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

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

print(reverse)

yadsruht si yadot

The above is the detailed content of 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