Python lists questions

What are the main differences between lists and tuples?
The main difference between the lists and tuples is that the tuples are immutable objects and lists are mutable. So, tuples cannot be modified while the lists can be modified. Tuples are more memory efficient than the lists and hence are typically faster
How to remove duplicates from a list?
Starting with Python 3.7, dictionary is insertion-ordered. So, dict.fromkeys(mylist) will remove the duplicates from a list preserving the original order of the list.
items = [1, 2, 0, 1, 3, 2]
list(dict.fromkeys(items))
[1, 2, 0, 3]
To remove the duplicates from a list and the original order of the list need not be preserved, assigning the list to a set and then converting it back to list will remove all the duplicates from the list (sets will not necessarily maintain the order of a list)
items = [1, 2, 0, 1, 3, 2]
list(set(items))
[0, 1, 2, 3]
What are the main differences between shallow copy and deep copy?
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances)
Shallow copying duplicates as little as possible. A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
Deep copying replicates the original at a different memory location, so the copy is totally independent with no connection to the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original. So, any changes made to the copy do not reflect in the original and any changes made to the original do not reflect in the copy.
What is a list?
A list is a mutable, ordered collection of objects. The objects can be of any type and need not be unique. Lists can be constructed by enclosing comma separated objects in a pair of square brackets or by using list constructor, list(iterator). An empty list can be constructed by using a pair of square brackets ([]).
Can a list be a member in itself?
Yes, but it will create a circular reference
Which code snippet will give this output:
['H', 'e', 'l', 'l', 'o']
break("Hello")
split("Hello")
unpack("Hello")
list("Hello")
Which of the following is NOT a difference between deep copy and shallow copy?
shallow copy shares child object references between copies, deep copy doesn't
deep copy can be created without importing copy module, but shallow copy can be created only after importing copy module
for large volumes of data, shallow copy is faster than deep copy
changes in the nested objects of compound objects copied using shallow copy will be reflected in other copies while changes in the nested objects of compound objects copied using deep copy will not be reflected in other copies
Which of the following is NOT a difference between extend and append list methods in Python?
After successful execution of append method on a list, the length of the list will increase by one. After successful execution of extend method on a list, the length of the list will increase by however many elements were in the iterable argument.
append() takes one argument and it can be an iterator or other data types. extend() takes one argument and it has to be an iterator
append modifies the list in-place while extend returns the modified list leaving the original list unchanged
The resulting list will become nested because of append method if another list is appended. The resulting list will not be nested because of append method.
What is the difference between a list and a tuple?
list is mutable and tuple is immutable
list typically uses more memory compared to tuple
list is generally slower than tuple for processing large volume of data
All of the above
Which of the following is NOT a difference between sort( ) and sorted( ) in Python?
sort( ) is a built-in function and sorted( ) is a method
sort() returns None and sorted( ) returns the sorted iterator
sort() sorts in-place, so the original order is lost and sorted() returns sorted iterator leaving the original and its order unchanged
sort() can be used with lists only and sorted() can be used with any iterator
A list called orig was created as shown below:
orig = [1, 2, [3, 4]]
Which of the following is NOT a way to create a shallow copy called new from orig?
new = list(orig)
new = orig[:]
new = orig.extend( )
new = [i for i in orig]
What would be the result of executing the following code?
list({'a':1,'A':2,'a':3})
['a', 1, 'A', 2]
['a', 1, 'A', 2, 'a', 3]
[1, 2, 3]
['a', 'A']
Python Interview Questions
  • Python - Lists
    1. What is the result of the following operation: 'A,B,C,D'.split(',')
    A) ['A','B','C','D']
    B) ('A','B','C','D')
    C) 'A','B','C','D'

    Answer: ['A','B','C','D']

    Explanation: Split returns a list of the words in the string, separated by the delimiter string, in this case, a comma.
    2. The method append does the following:
    A) adds one element to a list
    B) merges two lists
    C) inserts multiple elements to a list

    Answer: adds one element to a list

    Explanation: append-only adds one element.
    3. Lists are mutable
    A) True
    B) False

    Answer: True

    Explanation: Lists are mutable tuples are not
    4. Consider the following list : A=["hard rock",10,1.2] what will list A contain affter the following command is run: del(A[1])
    A) [10,1.2]
    B) ["hard rock" 1.2]
    C) ["hard rock", 10]

    Answer: ["hard rock" 1.2]

    Explanation: We will delete element 1
    5. What is the syntax to clone the list A and assign the result to list B
    A) B = A
    B) B=A[:]
    C) a = b

    Answer: B=A[:]

    Explanation: The [:] method is used to clone a list.
    6. Create num_list that contains the numbers 1 through 3?
    A) num_list = (1,2,3)
    B) num_list = [1,2,3]
    C) num_list = {1,2,3}

    Answer: num_list = [1,2,3]
    7. Create the list one_to_ten that holds values 1 through 10 with range()?
    A) one_to_ten = list(range(1,11))
    B) one_to_ten = list(range(10))
    C) one_to_ten = list[range(10)]

    Answer: one_to_ten = list(range(1,11))
    8. Print the 1st value in rand_list?
    A) rand_list[0]
    B) print(rand_list[1])
    C) print(rand_list[0])

    Answer: print(rand_list[0])
    9. Get the number of values in rand_list?
    A) len(rand_list)
    B) length(rand_list)
    C) size(rand_list)

    Answer: len(rand_list)
    10. Check if dog is in rand_list?
    A) dog in rand_list
    B) find("dog", rand_list)
    C) "dog" in rand_list

    Answer: "dog" in rand_list
    11. Get the index for dog in rand_list?
    A) rand_list.index("dog")
    B) index("dog", rand_list)
    C) "index(rand_list, "dog")

    Answer: rand_list.index("dog")
    12. Create even_list which will contain even numbers up till 10?
    A) even_list = [i*2 for i in range(5)]
    B) even_list = [i*2 range(10)]
    C) even_list = [i%2 for i in range(10)]

    Answer:even_list = [i*2 for i in range(5)]
    13. Create a multidimensional list list_table with 10 rows and columns?
    A) list_table = [10 for i in range(10)]
    B) list_table = [[0] * 10 for i in range(10)]
    C) list_table = [[0] % 10 for i in range(10)]

    Answer: list_table = [[0] * 10 for i in range(10)]
    14. What method here does not add one or more elements to a list?
    A) extend
    B) add
    C) append
    D) insert

    Answer: add

    Explanation: There is no add method
    15. Given a list, numbers = [1,2,3], how would you access the first element in the list?
    A) numbers[-1]
    B) numbers[1]
    C) numbers[0]

    Answer: numbers[0]
    16. Given a list numbers = [1,2,3] what would the result of numbers.pop(5) be?
    A) None
    B) IndexError
    C) 3

    Answer: IndexError
    17. Which of the following is not true about lists:
    A) You can loop over them using a for loop or a while loop
    B) The index starts at 1
    C) They are ordered collection of elements

    Answer: The index starts at 1
    18. Given a list numbers = [1,2,3,4] - what does numbers[::-1] return?
    A) [1,2,3,4]
    B) [1,4]
    C) [4,3,2,1]

    Answer: [4,3,2,1]
    19. Given a list numbers = [1,2,3,4] - what does numbers[1:3] return?
    A) [2,3]
    B) [1,2,3]
    C) [2,3,4]
    D) [3]

    Answer: [2,3]
    20. Given a list numbers = [1,2,3,4] - what does numbers[-2] return?
    A) [3]
    B) 3
    C) [1,2,3]
    D) [4]

    Answer: 3
  • gk