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]
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 iteratorsort() 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']
|