1. Home
  2. Docs
  3. Exercises
  4. List Exercises in Python

List Exercises in Python

List Exercises

 

In this section List Exercises are given. Learn about lists here.

Try these on your own. and then checkout the solutions.

 

 

Exercise 1: Given 2D array calculate the sum of diagonal elements.

Ex: [[1,3,5],[1,4,6],[7,6,9] => sum of 1 + 4 + 9 => 14

 
Solution:
 


#sum of diagonal elements

a_2d = [[1,2,3],[4,5,6],[7,8,9]]

b_2d = [[1,0,1],[1,1,0],[1,1,1]]

c_2d = [[2,0],[0,2]]

def sum_of_diagonal(a):

    sum = 0

    for i in range(len(a)):

        sum += a[i][i]

    return sum
        
print("sum a_2d: ",sum_of_diagonal(a_2d))

print("sum b_2d: ",sum_of_diagonal(b_2d))

print("sum c_2d: ",sum_of_diagonal(c_2d))



 

Exercise 2: From given list

gadgets = [“Mobile”, “Laptop”, 100, “Camera”, 310.28, “Speakers”, 27.00,
“Television”, 1000, “Laptop Case”, “Camera Lens”]

a)create separate lists of strings and numbers.
 
b)Sort the strings list in ascending order
 
c)Sort the strings list in descending order
 
d)Sort the number list from lowest to highest
 
e)Sort the number list from highest to lowest

 
Solution:
 

gadgets = ["Mobile", "Laptop", 100, "Camera", 310.28, "Speakers", 27.00, 
           "Television", 1000, "Laptop Case", "Camera Lens"]

str_items = []

num_items = []


for item in gadgets: 
    
    if isinstance(item, str):
        
        str_items.append(item)
        
    elif isinstance(item,int) or isinstance(item, float):
        
        num_items.append(item)


#a)creating separate lists

print(str_items)

print(num_items)

#b)sorting strings list in ascending order, a-z

str_items.sort(key=str.lower)

print(str_items)


#c)sorting strings list in descending order z-a

str_items.sort(key=str.lower, reverse=True)

print(str_items)


#d)sorting number list from lowest to highest

num_items.sort()

print(num_items)


#e)sorting number list from highest to lowest

num_items.sort(reverse=True)

print(num_items)

 
Exercise 3: Get first, second best scores from the list.

List may contain duplicates.

Ex: [86,86,85,85,85,83,23,45,84,1,2,0] => should get 86, 85
 
Solution:
 

#This is one of many possible solution

a_list = [86,86,85,85,85,83,23,45,84,1,2,0]

# method 1

def second_best(a):
    if a:

        first = a[0]

        second = None

        for i in range(1,len(a)):

            if second == None or second == first:

                second = a[i]

            if second < a[i]:

                second = a[i]

            if second > first:

                first, second = second, first

        return first, second

    else:

        return None, None

#function called     

f,s  = second_best(a_list)

print("First : {0} Second : {1}".format(f,s))



#method 2

#sort the list and get the elements

a_list = [84,84,86,86,85,85,85,83,23,45,84,1,2,0]

def first_second(given_list):

    a = given_list   #make a copy

    a.sort(reverse=True)

    print(a)

    first = a[0]

    second = None

    for element in a_list:

        if element != first:

            second = element

            return first, second

#function called

f,s = first_second(a_list)

print(f,s)