Top Python Interview Questions

Python Interview Questions

Question 1: How to create list and perform different operations on it?

Question 2: Can you add items of different data types in a list? What will be the output of following code snippet?

Answer: You can add items of different data types in a list. Below code shows items with different data types added in list and then printed. 

 1 2 3 4
 list = [] list.insert(1, 1) list.insert(2, "abc") print(list) 
 Output: [1, 'abc'] 

 

Question 3: What happens if you try to delete an item from list using index that doesn’t exist in list?

Answer: It gives an error if you try to delete an item using invalid index. 

 1 2 3 4 5
 list = [] list.insert(1, 1) list.insert(2, "abc") list.remove(3) print(list) 
 Output: ValueError: list.remove(x): x not in list 

 

Question 4: What happens if you add an element at an index where an elements already exists?

Answer: The element is inserted without an error and all existing elements get shifted one place. 

In below example “xyz” is inserted at index 1 where “abc” already exists. Output shows elements at index 1 and 2 gets shifted. 

 1 2 3 4 5 6
 list = [] list.insert(0, 1) list.insert(1, "abc") list.insert(2, "hello") list.insert(1, "xyz") print(list) 
 Output: [1, 'xyz', 'abc', 'hello'] 

 

Question 5: What happens if you try to sort list having mixed elements of type int and string?

Answer: Error is thrown if user tries to sort list having mixed types of int and string elements.

 1 2 3 4 5 6
 list = [] list.insert(0, 1) list.insert(1, "abc") list.insert(2, "hello") list.insert(3, "xyz") list.sort() 
 Output: TypeError: '<' not supported between instances of 'str' and 'int' 

 

Question 6: What happens if you try to reverse list having mixed elements of type int and string? 

Answer: list gets successfully reversed with mixed elements of type int and string. 

 1 2 3 4 5 6 7
 list = [] list.insert(0, 1) list.insert(1, "abc") list.insert(2, "hello") list.insert(3, "xyz") list.reverse() print(list) 
 Output: ['xyz', 'hello', 'abc', 1] 

 

Question 7: What are the different ways to convert a list of strings to a list of ints? for example – You are given a list something like [‘5′,’6′,’7′,’8′,’9’] and you need to convert this into [5,6,7,8,9]. 

Answer: There are different ways this can be accomplished. 

Approach 1 – Iterate over the list and convert each element to int

 1 2 3 4 5 6
 numbers = '5 6 7 8 9' split_list = numbers.split() print(split_list) for i in range(len(split_list)):     split_list[i] = int(split_list[i]) print(split_list) 
 Output: ['5', '6', '7', '8', '9'] [5, 6, 7, 8, 9] 

 

Approach 2 – Using map function

 1 2 3 4 5
 numbers = '5 6 7 8 9' split_list = numbers.split() print(split_list) split_list = map(int, split_list) print(list(split_list)) 
 Output: ['5', '6', '7', '8', '9'] [5, 6, 7, 8, 9] 

 

Approach 3 – Using list composition

 1 2 3 4 5
 numbers = '5 6 7 8 9' split_list = numbers.split() print(split_list) split_list = [int(i) for i in split_list] print(list(split_list)) 
 Output: ['5', '6', '7', '8', '9'] [5, 6, 7, 8, 9] 

 

{jcomments on}


Leave a Reply

Your email address will not be published. Required fields are marked *