Introduction
In this tutorial, we will look at top 15 List comprehension interview questions and answers (2023). In Python, List comprehension provides a concise and Pythonic way to work with Lists and other iterable data types, making your code more readable and efficient. We will look at 15 examples in the next section which are solved using List comprehension.
List Comprehension Overview
List Comprehension is a concise and efficient way to create Lists in Python. It allows you to generate a new Lists by applying an expression to each item in an existing iterable(e.g a List, Tuple or a String) and optionally filtering the items based on a condition. List comprehensions are a powerful and readable way to perform common operations on Lists, such as mapping, filtering and transformation.
Syntax of List Comprehension
new_list = [expression for item in iterable if condition]
List comprehension can be used in many ways Such as:
- Creating Lists
- Filtering elements from a List
- Transforming elements is a List
- Nested List comprehensions.
NOTE:
Top 15 List Comprehension Interview Questions and Answers (2023)
Also read: How to Install Flask in VS Code and Build REST API : [ 9 Easy Steps]
Question-1: Create a List of Cubes
In this problem, you will be asked to create a List which will contain cubes of numbers from 1 to 12 using List comprehension. Print the new List. Below is the solution.
cubes_list = [x**3 for x in range(1, 13)] print(cubes_list)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728]
Question-2: Generate List of Odd Numbers
In this problem, you will be asked to create a list which will contain elements of odd numbers from 1 to 20. Print the new List. Below is the solution of this problem.
odd_list = [x for x in range(1, 21) if x%2 !=0] print(odd_list)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Question-3: Create List of Length of Words
In this problem, a list will be given to you. You will be asked to create a new list which will contain double the length of words using string comprehension. Print the new list. Below is the solution.
word_list = ["Alice", "Alexander", "Anamika", "RajKumar", "Brian"] word_length_list = [2*(len(x)) for x in word_list ] print(word_length_list)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py [10, 18, 14, 16, 10]
Question-4: Create List of Prime Numbers
In this problem you will be asked to create a list of prime numbers from 1 to 30 using List comprehension and print the List. Below is the solution.
def isPrime(num): if num <= 1: return False for i in range(2, int(num**0.5) + 1): if num & i == 0: return False return True prime = [x for x in range(1, 30) if isPrime(x)] print(prime)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py [2, 3, 6, 7, 10, 11, 14, 15, 22, 23]
Question-5: Create Lists of Positive and Negative Numbers
In this problem, you will be given a list. You have to create two new list from the given list. First list will contain only the positive numbers from the given list. Second list will contain only the negative numbers from the given List. Below is the solution.
number = [2, -1, 3, 12, -10, -5, 7, 8, -3, 13, 15, -6] positive_number = [x for x in number if x > 0 ] negative_number = [x for x in number if x < 0 ] print(positive_number) print(negative_number)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py [2, 3, 12, 7, 8, 13, 15] [-1, -10, -5, -3, -6]
Question-6: Create List of Upper Case words
In this problem, you will be given a List of strings. You will be asked to create a new list which will contain words from the given list in upper case. Do this exercise using List comprehension. Print the new List. Below is the solution given.
word_list = ["Alice", "Alexander", "Anamika", "RajKumar", "Brian"] upper_word_list = [x.upper() for x in word_list] print(upper_word_list)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py ['ALICE', 'ALEXANDER', 'ANAMIKA', 'RAJKUMAR', 'BRIAN']
Question-7: Generate List of tuples
In this problem, you will be given two Lists. Your job will be to create a new List whose elements will be tuples. Each tuple will be a pair of numbers from the given two list such that their sum is even. Below is the solution.
num1_list = [1, 2,3] num2_list = [4,2,1] new_list = [(x,y) for x in num1_list for y in num2_list if (x+y) % 2 == 0] print(new_list)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py [(1, 1), (2, 4), (2, 2), (3, 1)]
Question-8: Create List of Squares and Cubes
In this problem, you will be asked to create a list whose elements will square of a number if number is even else cube of that number. Do this for the numbers from 2 to 12. Below is the solution given
number_list = [x**2 if x % 2 == 0 else x**3 for x in range(2, 13)] print(number_list)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py [4, 27, 16, 125, 36, 343, 64, 729, 100, 1331, 144]
Question-9: Create List of Strings having Five Characters
In this problem, you will be given a List. You have to create a new List whose elements will be only those strings from the given List whose length is below or equal to 5. Below is the solution given
word_list = ["Alice", "Alexander", "Aena", "RajKumar", "Brian", "Rony", "DollyBab", "Ankush"] new_list = [x for x in word_list if len(x) <= 5] print(new_list)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py ['Alice', 'Aena', 'Brian', 'Rony']
Question-10: Create List of Factor of 4
In this question, you will be asked to create a new list whose elements will be factor of number 4. Below is the solution given.
num = 4 factor_list = [x for x in range(1, num+1) if num % x == 0] print(factor_list)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py [1, 2, 4]
Question-11: Create a List from 2D Matrix
In this problem, you will given a 2D matrix. Your job is to create a new list which will only contain even numbers as elements. Below is the solution given
matrix = [[2,4,3, 12], [1,7,6,8,9]] new_list = [x for row in matrix for x in row if x % 2 == 0] print(new_list)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py [2, 4, 12, 6, 8]
Question-12: Create List of Reversed Strings
In this problem, you will be given a List of strings. You have to create a new List to store each of the strings from given List in reversed order in the new List. below is the solution.
word_list = ["Alice", "Alexander", "Anamika", "RajKumar", "Brian", "Dolly", "Drek"] new_list = [x[::-1] for x in word_list] print(new_list)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py ['ecilA', 'rednaxelA', 'akimanA', 'ramuKjaR', 'nairB', 'ylloD', 'kerD']
Question-13: Create List of Vowels
In this problem, you will be given a string. You have to create a new List which will contain only the vowels from the given string. Below is the solution given.
list_str = "Welcome To LinuxNasa World !" new_list = [x for x in list_str if x.lower() in 'aeiou'] print(new_list)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py ['e', 'o', 'e', 'o', 'i', 'u', 'a', 'a', 'o']
Question-14: Create List of Multiplication
In this problem, you will be given two integer List. Your job is to create a new List that contains product of elements at the same index. Below is the solution given
list1 = [2, 4, 6, 3] list2 = [5, 7, 9, 2] new_list = [x * y for x, y in zip(list1, list2) ] print(new_list)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py [10, 28, 54, 6]
Question-15: Create List of Unique Elements
In this problem you will be given a list. Your job is to create a new List which will contain all the unique elements. Below is the solution given.
list1 = [2, 4, 6, 3, 2, 9, 7, 7, 10, 2, 12, 5, 5] new_list = [x for i, x in enumerate(list1) if x not in list1[:i]] print(new_list)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\list-comp.py [2, 4, 6, 3, 9, 7, 10, 12, 5]
Summary
You can learn more about List and List comprehension from python.org.