Posts

Showing posts with the label Python List Programs

Python Sort List

Image
Python Program to Sort the List According to the Length of the Words Hey There Python Geeks , In this python tutorial , I have shown how to sort the list according to the length of the words. Consider a python list : list =['bye bye bye','hi','good morning','YouTube'] Take an empty list : empty_list = [ ] Take another empty list : final_list = [ ] Now, we need to find the length of the words which are there in the list and that can be done as : for i in list: For every word in the list, we need to add the length of the words to the empty_list : empty_list.append(len(i)) We have every word length in empty_list ,we need sort the empty_list ,so that we can have the words in order of low to high : empty_list.sort() Take for loop for every element in empty_list and another for loop for every element in list : for i in empty_list:                           ...

Python Program to Swap First and Last Element of a Given List

Image
Python Program to Swap First and Last Element of a Given List Hey Python Geeks , In this python tutorial ,  I have shown how to swap first and last element of a list. Consider a python list : list = [1,2,3,4,5,6,7,8,9] Create a python function called Swap that takes above python list as input, you can name it whatever you want and that can be written as : def Swap(list): Now, we have to swap first element to last element and that can be done as : list[0],list[-1]=list[-1], list[0] list[0] is the first element of the list list[-1] is the last element of the list We swapped them successfully, now call the function to see the swapped list and that can be done as : Swap(list) The code can be written as :             list = [1,2,3,4,5,6,7,8,9]             print("List before swapping",list)             def swap(list): ...

Python Program to Put Even and Odd Elements of a List into Two Different Lists

Image
  Hey There, In this blog, I have shown how to put even an odd elements of a list into two different lists. Consider the given list as : list = [1,2,3,4,5,6,7,8,9] Take list for even elements : even_list = [ ] Take list for odd elements = odd_list = [ ] To check each element in the given list we will use for loop, that can be written as : for i in list: To know a element is even, we divide it by 2. If element remainder is zero then it is a even number. That can be written as : if i%2 == 0: If that element is divisible by 2, then we add that element to the even_list and can be written as : even_list.append( i ) If it is not divisible by 2, then we append it to odd_list : odd_list.append( i ) So, the program can be written as :       list = [1,2,3,4,5,6,7,8,9]       even_list = [ ]       odd_list = [ ]       for i in list:            if...

Python Program to Find the Largest Number in a Given List

Image
  Hey There, In this video, I have shown how to find the largest number in a given list. Consider the given list as : list = [2,4,6,8,1,9,3,0] Now,sort the list to get the largest number at the last. The sorting can be done as : list.sort() After sorting the list look like  : list = [0,1,2,3,4,6,8,9] We got the largest number in the list at the end. To print the last element in the list, we can write the code as : print(list[-1]) So,the code can be written as :       list =  [2,4,6,8,1,9,3,0]       list.sort()       b = list[-1]       print("Largest number in the given list is : ",b) Output : Python Program to Find the Largest Number in a Given List Video Tutorial:

Python Program to Calculate the Average of Numbers in a Given List

Image
  Hey There, In this blog, I have shown how to calculate average of numbers in a given list.Average can be calculated as :                           Average = (Sum of all the numbers)/(Total numbers) Here the given list is :  list = [1,2,3,4,5,6,7,8,9] Sum can be calculated as :  sum_all = sum(list) Total numbers can be calculated as :  length = len(list) Now, Average will be :   avg = (sum_all/length) Printing the Average as :    print("Average is : ",avg) The above code can be written in a single line as :                          print("Average of numbers is : ", sum(list)/len(list)) Output :  Python Program to Calculate the Average of Numbers in a Given List Video Tutorial: