100% found this document useful (2 votes)
3K views

List Exercise PYTHON

This document provides 23 Python programming problems and their solutions. The problems cover a range of tasks involving lists such as summing and multiplying list items, finding minimum and maximum values, checking for common items between lists, removing items, shuffling lists, and more. For each problem, the solutions demonstrate how to use common Python list methods like append, index, join, etc. to manipulate and process lists.

Uploaded by

AmarSunuwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
3K views

List Exercise PYTHON

This document provides 23 Python programming problems and their solutions. The problems cover a range of tasks involving lists such as summing and multiplying list items, finding minimum and maximum values, checking for common items between lists, removing items, shuffling lists, and more. For each problem, the solutions demonstrate how to use common Python list methods like append, index, join, etc. to manipulate and process lists.

Uploaded by

AmarSunuwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1. Write a Python program to sum all the items in a list.

2. Write a Python program to multiples all the items in a list.

3. Write a Python program to get the largest number from a list.

4. Write a Python program to get the smallest number from a list.

5. Write a Python program to count the number of strings where the


string length is 2 or more and the first and last character are same
from a given list of strings.

Sample List : ['abc', 'xyz', 'aba', '1221']


Expected Result : 2

6. Write a Python program to get a list, sorted in increasing order by


the last element in each tuple from a given list of non-empty tuples.

Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Expected Result : [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]

7. Write a Python program to remove duplicates from a list.

8. Write a Python program to check a list is empty or not.

9. Write a Python program to clone or copy a list.

10. Write a Python program to find the list of words that are longer
than n from a given list of words.

11. Write a Python function that takes two lists and returns True if
they have at least one common member.

12. Write a Python program to print a specified list after removing the
0th, 2nd, 4th and 5th elements.
Sample List : ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
Expected Output : ['Green', 'White', 'Black']

13. Write a Python program to generate a 3*4*6 3D array whose each


element is *.

14. Write a Python program to print the numbers of a specified list


after removing even numbers from it.

15. Write a Python program to shuffle and print a specified list.

16. Write a Python program to generate and print a list of first and last
5 elements where the values are square of numbers between 1 and 30
(both included).

17. Write a Python program to generate and print a list except the first
5 elements, where the values are square of numbers between 1 and 30
(both included).

18. Write a Python program to get the difference between the two
lists.

19. Write a Python program access the index of a list.

20. Write a Python program to convert a list of characters into a


string.

21. Write a Python program to find the index of an item of a specified


list.

22. Write a Python program to append a list to second list.

23. Write a Python program to select an item randomly from a list.

Answers
Prog 1:

 def sum_list(items):
 sum_numbers = 0
 for x in items:
 sum_numbers += x
 return sum_numbers

 print(sum_list([1,2,-8]))

Prog 2:

 def multiply_list(items):
 tot = items[0]
 for x in items:
 tot *= x
 return tot

 print(multiply_list([1,2,-8]))

Prog 3:

 def max_num_in_list( list ):


 max = list[ 0 ]
 for a in list:
 if a > max:
 max = a
 return max

 print(max_num_in_list([1, 2, -8, 0]))

Prog 4:
1. def smallest_num_in_list( list ):
2. min = list[ 0 ]
3. for a in list:
4. if a < min:
5. min = a
6. return min
7. print(smallest_num_in_list([1, 2, -8, 0]))
Prog 5:

 def match_words(words):
 ctr = 0

 for word in words:
 if len(word) > 1 and word[0] == word[-1]:
 ctr += 1
 return ctr

 print(match_words(['abc', 'xyz', 'aba', '1221']))

Prog 6:

 def last(n): return n[-1]



 def sort_list_last(tuples):
 return sorted(tuples, key=last)

 print(sort_list_last([(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]))

Prog 7:
1. a = [10,20,30,20,10,50,60,40,80,50,40]
2.
3. dup_items = set()
4. uniq_items = []
5. for x in a:
6. if x not in dup_items:
7. uniq_items.append(x)
8. dup_items.add(x)
9.
10. print(dup_items)
Prog 8:

 l = []
 if not l:

 print("List is empty")

Prog 9:

1. original_list = [10, 22, 44, 23, 4]


2. new_list = list(original_list)
3. print(original_list)
4. print(new_list)

Prog 10:

 def long_words(n, str):


 word_len = []
 txt = str.split(" ")
 for x in txt:
 if len(x) > n:
 word_len.append(x)
 return word_len

print(long_words(3, "The quick brown fox jumps over the lazy dog"))

Prog 11:

 def common_data(list1, list2):


 result = False
 for x in list1:
 for y in list2:
 if x == y:
 result = True
 return result
 print(common_data([1,2,3,4,5], [5,6,7,8,9]))

 print(common_data([1,2,3,4,5], [6,7,8,9]))
Prog 12:
1. color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
2. color = [x for (i,x) in enumerate(color) if i not in (0,4,5)]
3. print(color)

Prog 13:

array = [[ ['*' for col in range(6)] for col in range(4)] for row in range(
3)]

 print(array)

Prog 14:
1. num = [7,8, 120, 25, 44, 20, 27]
2. num = [x for x in num if x%2!=0]
3. print(num)

Prog 15:

 from random import shuffle


 color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
 shuffle(color)

 print(color)

Prog 16:

 def printValues():
 l = list()
 for i in range(1,21):
 l.append(i**2)
 print(l[:5])
 print(l[-5:])

 printValues()
Prog 17:
1. def printValues():
2. l = list()
3. for i in range(1,21):
4. l.append(i**2)
5. print(l[5:])
6.
7. printValues()

Prog 18:

 list1 = [1, 2, 3, 4]
 list2 = [1, 2]

 print(list(set(list1) - set(list2)))

Prog 19:

1. nums = [5, 15, 35, 8, 98]


2. for num_index, num_val in enumerate(nums):
3. print(num_index, num_val)

Prog 20:

1. s = ['a', 'b', 'c', 'd']


2. str1 = ''.join(s)
3. print(str1)

Prog 21:

1. num =[10, 30, 4, -6]


2. print(num.index(30))

Prog 22:

 list1 = [1, 2, 3, 0]
 list2 = ['Red', 'Green', 'Black']
 final_list = list1 + list2

 print(final_list)

Prog 23:

 import random
 color_list = ['Red', 'Blue', 'Green', 'White', 'Black']

 print(random.choice(color_list))

You might also like