Data Science & ML

Python Data Structures

Lists, dictionaries, sets, tuples, list comprehensions, generators, itertools

20 interview questionsยท
Junior
1

What is the fundamental difference between a list and a tuple in Python?

Answer

Lists are mutable (can be modified after creation) while tuples are immutable (cannot be modified). This immutability makes tuples hashable and usable as dictionary keys, unlike lists. Tuples are also slightly more performant in terms of memory usage and access speed.

2

Which method should be used to add an element at the end of a Python list?

Answer

The append() method adds a single element at the end of a list. It modifies the list in place and returns None. To add multiple elements, use extend() or the += operator. The insert() method allows adding at a specific position.

3

How to create an empty dictionary in Python?

Answer

An empty dictionary can be created with {} or dict(). The {} syntax is more concise and slightly faster. The {} notation creates an empty dictionary, while set() creates an empty set. For an empty set, you must use set() because {} is reserved for dictionaries.

4

What is the result of the expression [x**2 for x in range(5)]?

5

What characteristic distinguishes a set from other Python collections?

+17 interview questions

Master Data Science & ML for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free