Data Structures

Gaining proficiency in using lists, tuples, sets, and dictionaries.

Sure! Here are 25 questions on data structures in Python, focusing on lists, tuples, sets, and dictionaries, each accompanied by example code:

Lists

1

How do you create a list in Python?

my_list = [1, 2, 3, 4, 5]

2

How can you append an element to a list?

my_list = [1, 2, 3]
my_list.append(4)  # [1, 2, 3, 4]

3

How can you remove an element from a list by value?

my_list = [1, 2, 3, 4]
my_list.remove(3)  # [1, 2, 4]

4

How do you access the last element of a list?

my_list = [1, 2, 3, 4]
last_element = my_list[-1]  # 4

5

How can you slice a list to get the first three elements?


6

How do you sort a list in ascending order?


7

How can you find the index of a specific element in a list?


8

How do you concatenate two lists?


9

How can you count the occurrences of an element in a list?


10

How do you create a list of squares of numbers from 1 to 5?


Tuples

1

How do you create a tuple in Python?


2

How can you access an element in a tuple?


3

How do you find the length of a tuple?


4

How do you unpack a tuple into multiple variables?


5

Can you modify an element in a tuple?


6

How do you concatenate two tuples?


7

How do you repeat elements in a tuple?


8

How do you check if an element exists in a tuple?


9

How do you find the index of an element in a tuple?


10

How can you create a tuple with one element?


Sets

1

How do you create a set in Python?


2

How do you add an element to a set?


3

How do you remove an element from a set?


4

How do you find the intersection of two sets?


5

How do you find the union of two sets?


Dictionaries

1

How do you create a dictionary in Python?


2

How do you access a value by its key in a dictionary?


3

How can you add a new key-value pair to a dictionary?


4

How do you remove a key-value pair from a dictionary?


5

How do you check if a key exists in a dictionary?


6

How can you get all the keys from a dictionary?


7

How can you get all the values from a dictionary?


8

How do you iterate over a dictionary’s items?


9

How can you merge two dictionaries?


10

How do you create a dictionary with default values for missing keys?


11

How do you create a dictionary comprehension?


12

How do you get a default value if a key is not found in a dictionary?


13

How can you clear all items from a dictionary?


14

How do you copy a dictionary?


15

How do you update a dictionary with another dictionary?


Last updated