0% found this document useful (0 votes)
6 views3 pages

Python List Exercises and Solutions

Python exercise

Uploaded by

mehurvala
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Python List Exercises and Solutions

Python exercise

Uploaded by

mehurvala
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

A list is a container which holds comma-separated values (items or elements) between square

brackets where items or elements need not all have the same type.
You may read our Python list tutorial before solving the following exercises.
[An editor is available at the bottom of the page to write and execute the scripts.]
1. Write a Python program to sum all the items in a list.
.
2. Write a Python program to multiply 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 from a given list of strings. The string
length is 2 or more and the first and last characters are the same.
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 if 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, 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 the first and last 5 elements where the
values are square numbers between 1 and 30 (both included).
.
17. Write a Python program to generate and print a list except for the first 5 elements, where the
values are square numbers between 1 and 30 (both included).
.
18. Write a Python program to generate all permutations of a list in Python.
.
19. Write a Python program to calculate the difference between the two lists.
.
20. Write a Python program to access the index of a list.
.
21. Write a Python program to convert a list of characters into a string.
.
22. Write a Python program to find the index of an item in a specified list.
.
23. Write a Python program to flatten a shallow list.
.
24. Write a Python program to append a list to the second list.
.
25. Write a Python program to select an item randomly from a list.
.
26. Write a Python program to check whether two lists are circularly identical.
.
27. Write a Python program to find the second smallest number in a list.
.
28. Write a Python program to find the second largest number in a list.
.

29. Write a Python program to get unique values from a list.


.
30. Write a Python program to get the frequency of elements in a list.
Click me to see the sample

Common questions

Powered by AI

To sum all elements in a Python list, you can use the built-in sum() function, which iterates over the list elements and returns the total sum. When performing this operation, it is important to ensure that all elements are numeric, as the function will raise a TypeError if the list contains non-numeric data types.

To find the second smallest number in a list, sort the list and then return the second element. For example: `sorted_list = sorted(my_list); return sorted_list[1]`. Edge cases to consider include lists with fewer than two elements, which require handling to prevent IndexError.

An efficient way to check if a list is empty in Python is to use the implicit Boolean evaluation of the list, such as: `if not my_list:`. This method is preferred because it is concise and leverages Python's truthiness principles, where empty sequences evaluate to False.

To sort a list of tuples by the last element, you can use the sorted function with a lambda function as the key parameter: `sorted(list_of_tuples, key=lambda x: x[-1])`. This approach sorts the tuples based on the value of the last element in each tuple.

When needing to access the index of each element while iterating over a list, the built-in enumerate function is ideal. It returns a tuple of the index and the element during iteration, allowing easy access and manipulation: `for index, element in enumerate(my_list):`.

To convert a list of characters into a string, you can use the join method: `''.join(list_of_chars)`. This concatenates the characters in the list into a single string. An example use case is transforming a list of characters representing a word back into the original string format.

To clone or copy a list in Python, you can use list slicing: `new_list = original_list[:]`. This operation creates a new list with the same elements as the original, unlike creating a reference using `new_list = original_list`, which would have both variables pointing to the same list object in memory.

To find common elements between two lists, you can use the set intersection method. Convert both lists into sets and use the & operator: `set(list1) & set(list2)`. This method has a time complexity of O(n + m) where n and m are the lengths of the two lists.

To remove duplicate values from a list in Python while maintaining order, you can use collections.OrderedDict in combination with the dict.fromkeys method. This approach preserves the order of elements while efficiently filtering duplicates. For example: `list(OrderedDict.fromkeys(your_list))`.

A list in Python is defined as a container that holds comma-separated values (items or elements) between square brackets, where items or elements do not need to be of the same type. For example, a list can contain integers, strings, and other objects simultaneously.

You might also like