BASIC LIST OPERATION
how to find out the sum of all the elements
in a list:
In [3]: def find_sum(lst):
total=0
for num in lst:
total += num
return(total)
my_list=[1,2,3,4,5,6,7,8,9,10]
print(find_sum(my_list))
55
In [4]: # alternative sue in build in function
def find_sum(lst):
return(sum(lst))
my_list=[1,2,3,4,5,6,7,8,9,10]
print(find_sum(my_list))
55
how to find maximum element in a list
In [10]: def find_max(lst):
if not lst:
return None
max_item=lst[0]
for num in lst:
if num > max_item:
max_item = num
return(max_item)
my_list=[45,32,45,76,115,87,54,97]
#my_list=[]
print(find_max(my_list))
115
In [11]: # alternative sue in build in function
def find_max(lst):
if not lst:
return None
return max(lst)
my_list=[45,32,45,76,115,87,54,97]
#my_list=[]
print(find_max(my_list))
115
how to sort list in ascending order
In [15]: def lst_asce(lst):
if not lst:
return None
lst.sort()
return lst
my_list=[45,32,45,76,115,87,54,97]
#my_list=[]
print(lst_asce(my_list))
[32, 45, 45, 54, 76, 87, 97, 115]
In [16]: def lst_asce(lst):
if not lst:
return None
return sorted(lst)
my_list=[45,32,45,76,115,87,54,97]
#my_list=[]
print(lst_asce(my_list))
[32, 45, 45, 54, 76, 87, 97, 115]
merge two list
In [19]: def merge_lst(lst1,lst2):
return lst1 + lst2
lst1=[12,34,56,78,43]
lst2=[45,32,45,67,89]
print(merge_lst(lst1,lst2))
[12, 34, 56, 78, 43, 45, 32, 45, 67, 89]
In [20]: # alternate method
def merge_lst(lst1,lst2):
lst1.extend(lst2)
return lst1
lst1=[12,34,56,78,43]
lst2=[45,32,45,67,89]
print(merge_lst(lst1,lst2))
[12, 34, 56, 78, 43, 45, 32, 45, 67, 89]
LIST MANIPULATION
How to reverse a list in a place
In [22]: def reverse_lst(lst):
lst.reverse()
return lst
my_list=[45,32,45,76,115,87,54,97]
print(reverse_lst(my_list))
[97, 54, 87, 115, 76, 45, 32, 45]
In [25]: # alternate method
def reverse_lst1(lst):
return lst[::-1]
my_list=[45,32,45,76,115,87,54,97]
print(reverse_lst1(my_list))
[97, 54, 87, 115, 76, 45, 32, 45]
how to remove duplicate from list
In [24]: def remove_dupli(lst):
return list(set(lst))
my_list= [23,45,67,89,43,56,23,67,54,12,89]
print(remove_dupli(my_list))
[67, 43, 12, 45, 54, 23, 56, 89]
In [27]: # alternate method
def remove_duplic(lst):
seen=set()
new_list=[]
for num in lst:
if num not in seen:
seen.add(num)
new_list.append(num)
return new_list
my_list= [23,45,67,89,43,56,23,67,54,12,89]
print(remove_duplic(my_list))
[23, 45, 67, 89, 43, 56, 54, 12]
how to remove the occurances of specific
element from a list
In [5]: def remove_ele(lst,element):
return[ x for x in lst if x != element]
my_list = [23,45,6,5,7,8,9,43,21]
element=int(input('Enter the element that remove:'))
print(remove_ele(my_list , element))
Enter the element that remove:21
[23, 45, 6, 5, 7, 8, 9, 43]
In [7]: # alternate method
def remove_elem(lst,ele):
i=0
while i < len(lst):
if lst[i] == ele:
lst.pop(i)
else:
i+=1
return lst
my_list = [23,45,6,5,7,8,9,43,21]
element=int(input('Enter the element that remove:'))
print(remove_elem(my_list , element))
Enter the element that remove:43
[23, 45, 6, 5, 7, 8, 9, 21]
how to remove the first and last element
from the list
In [8]: def remove_fir_lst(lst):
if len(lst) <= 2:
return None
return lst[1:-1]
my_list = [23,45,6,5,7,8,9,43,21]
print(remove_fir_lst(my_list))
[45, 6, 5, 7, 8, 9, 43]
In [10]: # alternative method
def remove_fir_lst(lst):
if len(lst) <= 2:
lst.clear()
else:
lst.pop(0) # remove 1st element
lst.pop() # remove 2nd element
return lst
my_list = [23,45,6,5,7,8,9,43,21]
print(remove_fir_lst(my_list))
[45, 6, 5, 7, 8, 9, 43]
how to split a list into chunks of a given
size
In [15]: def split_in_chunk(lst, chunk):
return [ lst[i:i + chunk ] for i in range(0, len(lst), chunk) ]
my_list=[1,2,3,4,5,6,7,8,9]
print(split_in_chunk(my_list , 3))
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
how to flattent list into single list
In [16]: def flatten(lst):
return [ item for sublist in lst for item in sublist]
my_list= [[1,2,34],[56,78,6],[12,443,56,9]]
print(flatten(my_list))
[1, 2, 34, 56, 78, 6, 12, 443, 56, 9]
how you rotate list to the left by given
number of position
In [22]: def rotate_lst(lst,position):
n = len(lst)
position %= n # handle the rotation larger than the length
return lst[position :] + lst[: position]
my_list=[1, 2, 34, 56, 78, 6, 12, 443, 56, 9]
position= 5
print( rotate_lst(my_list, position))
[6, 12, 443, 56, 9, 1, 2, 34, 56, 78]
list Comprehensions and Filtering
how to create new list containing only even
number from the original list.
In [23]: def even_list(lst):
return [ x for x in lst if x % 2 == 0]
my_list=[1, 2, 34, 56, 78, 6, 12, 443, 56, 9]
print(even_list(my_list))
[2, 34, 56, 78, 6, 12, 56]
Searching and counting
How to count the occurances of an
element in a list
In [25]: def occurance_element(lst, element):
return lst.count(element)
my_list = [23,45,6,5,7,9,45,43,21,8,23,6,5,7,8,9,43,21]
element= int(input("enter the element from list:,"))
print(occurance_element(my_list, element))
enter the element from list:,45
2
how to find the index of the first occurance
of an element in a list
In [28]: def index_first(lst,element):
try:
return lst.index(element)
except ValueError:
return -1
my_list=[1,2,34,56, 2, 78,89]
element = int(input('enter the index value fron given list:,'))
print(index_first(my_list,element))
enter the index value fron given list:,2
1
how to find the index of the last occurance
of an element in a list
In [29]: def index_lastt(lst,element):
try:
return len(lst) - 1 - lst[:: -1].index(element)
except ValueError:
return -1
my_list=[1,2,34,56, 2, 78,89]
element = int(input('enter the index value fron given list:,'))
print(index_lastt(my_list,element))
enter the index value fron given list:,2
4
how to find the second largest element
from list
In [50]: def second_largest(lst):
if len(lst) < 2 : # lenth not enough elements
return None
unique_element = sorted ( list(set(lst)), reverse = True) # remove duplicate
if len(unique_element) < 2 :
return None
return unique_element[1]
my_list=[1,2,34,56, 2, 78,88,89]
print(second_largest(my_list))
88
In [49]: # alternate method
def second_largest(lst):
if len(lst) < 2 : # lenth not enough elements
return None
max_element = lst[0]
second_max = 0
for i in lst:
if i > max_element:
second_max = max_element
max_element = i
elif i > second_max and i != max_element :
second_max = i
return second_max
my_list=[1,2,34,56, 2, 78,88,89]
print(second_largest(my_list))
88
how to find the comman element between
two list
In [52]: def comman_ele(lst1,lst2):
return list(set(lst1) & set(lst2))
my_list1=[1,2,34,56, 2, 78,47,45,10,29,88,89]
my_list2=[1,54,89,45,74,10,2,34,56, 2, 78,88,89]
print(comman_ele(my_list1,my_list2))
[1, 2, 34, 10, 45, 78, 56, 89, 88]
In [55]: # alternate method
def comman_ele(lst1,lst2):
return [x for x in lst1 if x in lst2 ]
my_list1=[1,2,34,56, 2, 78,47,45,10,29,88,89]
my_list2=[1,54,89,45,74,10,2,34,56, 2, 78,88,89]
print(comman_ele(my_list1,my_list2))
[1, 2, 34, 56, 2, 78, 45, 10, 88, 89]
other list related question
how to check given list is palindrome
In [60]: def is_palindrome(lst1):
return lst1 == lst1[::-1]
lst1= [1,2,3,2,1]
print(is_palindrome(lst1))
True
how to check all element in list are positive
In [58]: def is_positive(lst):
return all( x > 0 for x in lst1)
lst1= [1,2,3,2,1]
print(is_positive(lst1))
True
calculate the product of all element
In [64]: def product(lst):
product = 1
for i in lst:
product *= i
return product
lst1= [1,2,3,2,1]
print(product(lst1))
12
how to find difference between two list
In [66]: def diff(lst1,lst2):
return list(set(lst1) - set(lst2))
my_list1=[1,2,34,56, 2, 78,47,45,10,29,88,89]
my_list2=[1,54,89,45,74,10,2,34,56, 2, 78,88,89]
print(diff(my_list1,my_list2))
[29, 47]
find the longest sublist in a list
In [67]: def longest_sub_lst(lst):
if not lst:
return None
return max ( lst , key=len)
my_list2=[[1, 2, 3], [4, 5, 6], [7, 8, 21,78,9]]
print(longest_sub_lst(my_list2))
[7, 8, 21, 78, 9]
how to count odd and even number in list
In [71]: def odd_even_count(lst):
odd_count = 0
even_count = 0
for num in lst:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
return (f" in a given list odd number is : {odd_count} and even number is : { e
my_list2=[1,54,89,45,74,10,2,34,56, 2, 78,88,89]
print(odd_even_count(my_list2))
in a given list odd number is : 4 and even number is : 9
In [ ]:
In [ ]: