Some Basic List Programs-1


List is a changeable and ordered data type that allows it to be indexed and sliced. It can contain heterogeneous values such as integers, floats, strings, tuples, lists, and dictionaries but they are commonly used to store collections of homogeneous objects.

Python program to check if a list is empty or not

empty_list = []

if empty_list:
    print("List is not empty")
    print("List is empty")
#Output: List is empty
  • Python treats everything as an object and objects are always true in python. However, null values, None, 0, empty data types are observed as false in python.

Python program to create a list of empty dictionaries

num = 8 #no of empty dictionaries in list
list = [ {} for i in range(num)] #list comprehension
#Output: [{}, {}, {}, {}, {}, {}, {}, {}]

List comprehension is a compact method that defines a list and its contents at the same time. The basic syntax of list comprehension consists of a square bracket with an expression that is executed for each element along with for loop to iterate over each element.

Python program to find the sum of all the elements in a list

list = [11, 20, 4, 23, -9]

#using loops
sum = 0
for i in list: #iterating through each item in list
    sum = sum + i
#Output: 49

#using built-in sum() method
sum = sum(list)
#Output: 49
  • The sum() built-in method sums up all the numbers present in the list.

Python program to find the average of all the numbers in a list

The average of all the numbers in a list can be simply calculated by using the sum() and len() function.

  • The len() function returns the number of s in elements present in an object.
list = [7, 1, 16, 10, 12, 13]
average = int(sum(list) / len(list))
#Output: 9

Python program to find the second largest number in a list

list = [28, 77, 35, 52, 11] 

#using sort() built-in list method 
list.sort(reverse = True) #Sorting all the elements of a list in descending order
#Output: 52

#using set(), remove() and max() built-in methods
list_set = set(list)
list_set.remove(max(list_set)) # removing the largest element from the set
#Output: 52

Python program to get a random element from a list

The random module is used to generate random numbers in python. This module implements pseudo-random number generators for various distributions.

import random
list = [29, 1, 71, 12, 88, 6, 32, 58, 93, 67, 75, 43, 97, 24]

#using random.choice() 
random_num = random.choice(list)
#Output: 12

#using random.random()
random_num = int(random.random() * len(list))
#Output: 6 32

#Using random.randrange()
random_num = random.randrange(len(list))
#Output: 8 93

#Using random.randint() 
random_num = random.randint(0,len(list)-1) 
#Output: 4 88
  • The random.choice() returns a randomly selected element from a non-empty sequence.
  • The random.random() generates a random float number between 0.0 and 1.0. This random float number is then multiplied with the length of the list and typecasted to int to get an integer index value for the list.
  • The random.randrange()  generates a random number in a range specified in its argument. This generated random number is then used as an index of the list.
  • The random.randint()  generates a random integer between the specified integers. This generated random integer is then used as an index of the list.

Leave a Comment

%d bloggers like this: