lee.michele53
lee.michele53 4d ago • 0 views

Python List Comprehension Multiple Choice Questions

Hey everyone! 👋 Struggling a bit with Python List Comprehension? No worries, it's a super powerful and concise way to create lists, but it can feel a little tricky at first. I've put together a quick study guide and some practice questions to help you really master it. Let's get coding! 🚀
💻 Computer Science & Technology
🪄

🚀 Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

✨ Generate Custom Content

1 Answers

✅ Best Answer

📚 Quick Study Guide: Python List Comprehension

  • 💡 What it is: List comprehension offers a concise way to create lists based on existing iterables (like other lists, tuples, strings, or ranges) in a single line of code. It's often more readable and efficient than traditional `for` loops.
  • ✍️ Basic Syntax: The fundamental structure is [expression for item in iterable]. Here, the expression is evaluated for each item in the iterable, and the results form the new list.
  • Conditional Logic (Filtering): You can add a condition to filter items using [expression for item in iterable if condition]. Only items for which the condition is true will be included in the new list.
  • ⚖️ Conditional Logic (Transforming): For conditional transformations of items, you can use an if-else statement within the expression part: [expression_if_true if condition else expression_if_false for item in iterable].
  • 🏗️ Nested List Comprehension: You can use multiple for clauses for creating lists from nested iterables, similar to nested loops: [expression for item1 in iterable1 for item2 in iterable2].
  • Benefits: Often leads to more compact and readable code, and can be significantly faster than explicit for loops for certain operations due to internal optimizations.
  • 🧭 When to Use: Ideal for creating new lists by transforming or filtering elements from existing sequences. Avoid using it for complex logic that would make the single line unreadable.

📝 Practice Quiz

  1. What is the output of the following code?
    numbers = [1, 2, 3, 4, 5]
    squared_evens = [n*n for n in numbers if n % 2 == 0]
    print(squared_evens)


    A. [1, 9, 25]
    B. [4, 16]
    C. [2, 4]
    D. [1, 4, 9, 16, 25]
  2. Which of the following list comprehensions correctly generates a list of all uppercase letters from a given string `text = "Hello World"`?
    A. [char.upper() for char in text if char.islower()]
    B. [char for char in text if char.isupper()]
    C. [char for char in text if char.is_upper()]
    D. [char for char in text if char.upper()]
  3. Consider the code:
    words = ["apple", "banana", "cherry"]
    lengths = [len(word) for word in words]
    print(lengths)

    What will be printed to the console?
    A. [5, 6, 6]
    B. [5, 6, 7]
    C. ["apple", "banana", "cherry"]
    D. [len(word), len(word), len(word)]
  4. What is the output of this list comprehension?
    matrix = [[1, 2], [3, 4], [5, 6]]
    flattened = [num for row in matrix for num in row]
    print(flattened)


    A. [[1, 2], [3, 4], [5, 6]]
    B. [1, 2, 3, 4, 5, 6]
    C. [1, 3, 5, 2, 4, 6]
    D. [[1, 3, 5], [2, 4, 6]]
  5. Which list comprehension will create a list of numbers from 0 to 9, where even numbers are replaced by the string "Even" and odd numbers remain as they are?
    A. ["Even" if x % 2 == 0 for x in range(10) else x]
    B. [x if x % 2 != 0 else "Even" for x in range(10)]
    C. [x if x % 2 == 0 else "Even" for x in range(10)]
    D. ["Even" for x in range(10) if x % 2 == 0 else x]
  6. Given data = [10, -5, 20, -15, 0], what will be the result of:
    positive_numbers = [x for x in data if x > 0]
    print(positive_numbers)


    A. [10, 20, 0]
    B. [10, 20]
    C. [-5, -15]
    D. [10, -5, 20, -15, 0]
  7. What is the most Pythonic way to create a list of tuples, where each tuple contains a number and its cube for numbers from 1 to 3?
    A. my_list = []
    for i in range(1, 4):
    my_list.append((i, i3))

    B. [(i, i
    3) for i in range(1, 4)]
    C. list(map(lambda i: (i, i3), range(1, 4)))
    D. generate_tuples = lambda i: (i, i
    3)
    my_list = [generate_tuples(i) for i in range(1, 4)]
Click to see Answers

1. B
2. B
3. B
4. B
5. B
6. B
7. B

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀