# Python Data Structure Interview Questions

Displaying 1 - 10 of 98

## Python: Count Prime Numbers upto the given input number

09/19/2021 - 12:13

Code:

``````n=int(input("Enter the number till you want to check: "))
primes = []
for i in range (2, n+1):
for j in range(2, i):
if i%j == 0:
break
else:
primes.append(i)

print("Prime Numbers: ",primes)
print("Count of prime numbers: ", len(primes))``````

Output:

Enter the number till you want to check: 11
Prime Numbers:  [2, 3, 5, 7, 11]
Count of prime numbers:  5

## Is There a Way to Get a List of All the Keys in a Dictionary? If So, How Would You Do It?

06/24/2021 - 01:34

To obtain a list of all the keys in a dictionary, we have to use function keys(). Check below example:

``````my_dict={'k1':10,'k2':5,'k3':20,'k4':25}
keys_list = []

for key in my_dict.keys():
keys_list.append(key)

print(keys_list)``````

Output: ['k1', 'k2', 'k3', 'k4']

## Python: In a List and in a Dictionary, What Are the Typical Characteristics of Elements?

06/24/2021 - 01:33

Elements in lists maintain their ordering unless they are explicitly commanded to be re-ordered. They can be of any data type, they can all be the same, or they can be mixed. Elements in lists are always accessed through numeric, zero-based indices.

In a dictionary, each entry will have a key and a value, but the order will not be guaranteed. Elements in the dictionary can be accessed by using their key.

Lists can be used whenever you have a collection of items in an order. A dictionary can be used whenever you have a set of unique keys that map to values.

## In the below assignment operations, what is the value assigned to the variable d.

06/24/2021 - 01:25

Below code will output 4. This type of assignment is called Tuple Unpacking.

``````A = 1, 2, 3, 4
a, b, c, d = A
print(d)``````

## How will you remove the last object from a list?

06/24/2021 - 01:22

The pop function removes and returns the last object (or obj) from the list. Here -1 represent last element of the list. Below example will return 5.

``````my_list = [1, 2, 3, 4, 5]
last_element = my_list.pop(-1)
print(last_element)``````

## What is the difference between Python Arrays and lists?

06/24/2021 - 01:20

Arrays and lists, in Python, have the same way of storing data. But, arrays can hold only a single data type elements whereas lists can hold any data type elements.

``````import array as arr

my_array = arr.array('i', [1, 2, 3, 4])
my_list=[1, 'abc', 1.20]

print(my_array)
print(my_list)``````

## How will you reverse a list in Python?

06/24/2021 - 01:18

list.reverse(): This function reverses objects of list. For example:

``````my_list = [1, 3, 5, 6]
list.reverse(my_list)
print(my_list)``````

## In below Python code, modify the set2 so that set1.intersection(set2) returns {5, 7, 9, 12}

06/24/2021 - 01:15

Code:

``````set1 = {14, 5, 9, 31, 12, 77, 67, 8}
set2 = {5}``````

``````set1 = {14, 5, 9, 31, 12, 77, 67, 8}
set2 = {5, 77, 9, 12}
print(set1.intersection(set2))``````

## In Python what is slicing?

06/24/2021 - 01:14

A mechanism to select a range of items from sequence types like list, tuple, strings etc. is known as slicing. Example:

``````a = ("a", "b", "c", "d", "e", "f", "g", "h")
x = slice(2)
print(a[x])``````

Output: ('a', 'b')

## What Native Data Structures Can You Name in Python? Of These, Which Are Mutable, and Which Are Immutable?

06/24/2021 - 01:10

Common native data structures in Python are as follows:

• Dictionaries (Mutable)
• Lists (Mutable)
• Sets (Mutable)
• Strings (Immutable)
• Tuples (Immutable)

Strings and tuples are immutable, as their contents can't be altered once they're created.