Python List: Advanced - Exercises, Practice, Solution
Python Advanced List [15 exercises with solution]
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. Go to the editor]
1. Write a Python function to reverse a list at a specific location.
Click me to see the sample solution
2. Write a Python function find the length of the longest increasing sub-sequence in a list.
Click me to see the sample solution
3. Write a Python function that finds all the permutations of the members of a list.
Click me to see the sample solution
4. Write a Python function to find the kth smallest element in a list.
Click me to see the sample solution
5. Write a Python function to find the kth largest element in a list.
Click me to see the sample solution
6. Write a Python function to check if a list is a palindrome or not. Return true otherwise false.
Click me to see the sample solution
7. Write a Python a function to find the union and intersection of two lists.
Click me to see the sample solution
8. Write a Python function to remove duplicates from a list while preserving the order.
Click me to see the sample solution
9. Write a Python a function to find the maximum sum sub-sequence in a list. Return the maximum value.
Click me to see the sample solution
10. Write a Python a function to find the minimum sum sub-sequence in a list. Return the sub-sequence.
Click me to see the sample solution
11. Write a Python function to find the longest common sub-sequence in two lists.
Click me to see the sample solution
12. Write a Python program to find the first non-repeated element in a list.
Click me to see the sample solution
13. Write a Python a function to implement a LRU cache.
From Wikipedia -
Least recently used (LRU)
Discards the least recently used items first. This algorithm requires keeping track of what was used when, which is expensive if one wants to make sure the algorithm always discards the least recently used item. General implementations of this technique require keeping "age bits" for cache-lines and track the "Least Recently Used" cache-line based on age-bits. In such an implementation, every time a cache-line is used, the age of all other cache-lines changes. LRU is actually a family of caching algorithms with members including 2Q by Theodore Johnson and Dennis Shasha, and LRU/K by Pat O'Neil, Betty O'Neil and Gerhard Weikum.
Click me to see the sample solution
14. Write a Python function to sort a list of dictionaries based on values of a key.
Click me to see the sample solution
15. Write a Python program to find all the pairs in a list whose sum is equal to a given value.
Click me to see the sample solution
List: Cheat Sheet
Making a list:
colors = ['Red', 'Blue', 'Green', 'Black', 'White']
Accessing elements:
# Getting the first element first_col = colors[0] # Getting the second element second_col = colors[1] # Getting the last element newest_col = colors[-1]
Modifying individual items:
# Changing an element colors[0] = 'Yellow' colors[-2] = 'Red'
Adding elements:
# Adding an element to the end of the list colors.append('Orange') # Starting with an empty list colors = [] colors.append('Red') colors.append('Blue') colors.append('Green') # Inserting elements at a particular position colors.insert(0, 'Violet') colors.insert(2, 'Purple')
Removing elements:
# Deleting an element by its position del colors[-1] # Removing an item by its value colors.remove('Green')
Popping elements:
# Pop the last item from a list most_recent_col = colors.pop() print(most_recent_col) # Pop the first item in a list first_col = colors.pop(0) print(first_col)
List length:
# Find the length of a list num_colors = len(colors) print("We have " + str(num_colors) + " colors.")
Sorting a list:
# Sorting a list permanently colors.sort() # Sorting a list permanently in reverse alphabetical order colors.sort(reverse=True) # Sorting a list temporarily print(sorted(colors)) print(sorted(colors, reverse=True)) # Reversing the order of a list colors.reverse()
Looping through a list:
# Printing all items in a list for col in colors: print(col) # Printing a message for each item, and a separate message afterwards for col in colors: print("Welcome, " + col + "!") print("Welcome, we're glad to see you all!")
The range() function:
# Printing the numbers 0 to 2000 for num in range(2001): print(num) # Printing the numbers 1 to 2000 for num in range(1, 2001): print(num) # Making a list of numbers from 1 to a million nums = list(range(1, 1000001))
Simple statistics:
# Finding the minimum value in a list nums = [23, 22, 44, 17, 77, 55, 1, 65, 82, 2] num_min = min(nums) # Finding the maximum value nums = [23, 22, 44, 17, 77, 55, 1, 65, 82, 2] num_max = max(nums) # Finding the sum of all numbers nums = [23, 22, 44, 17, 77, 55, 1, 65, 82, 2] total_num = sum(nums)
Slicing a list:
# Getting the first three items colors = ['Red', 'Blue', 'Green', 'Black', 'White'] first_three = colors [:3] # Getting the middle three items middle_three = colors[1:4] # Getting the last three items last_three = colors[-3:]
Copying a list:
# Making a copy of a list colors = ['Red', 'Blue', 'Green', 'Black', 'White'] copy_of_colors = colors[:]
List of Comprehensions:
# Using a loop to generate a list of square numbers squr = [] for x in range(1, 11): sq = x**2 squr.append(sq) # Using a comprehension to generate a list of square numbers squr = [x**2 for x in range(1, 11)] # Using a loop to convert a list of names to upper case colors = ['Red', 'Blue', 'Green', 'Black', 'White'] upper_cols = [] for cols in colors: upper_cols.append(cols.upper()) # Using a comprehension to convert a list of names to upper case colors = ['Red', 'Blue', 'Green', 'Black', 'White'] upper_cols = [cols.upper() for cols in colors]
Python Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
Test your Python skills with w3resource's quiz
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/list-advanced/index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics