Python Interview Questions FAQ - 2
When does dictionary is used instead of a list?
You would only use the Dictionary if you needed to map (or associate) meaningful keys to values, whereas a List only maps (or associates) positions (or indices) to values.
How to sort a dictionary by key?
How to sort a dictionary by value?
What do you mean by list comprehension?
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
same as :
How to clone or copy a list?
Python 3.3+ adds list.copy() method, which should be as fast as slicing:
How to remove duplicates from a list ?
What is a built-in function that Python uses to iterate over a number sequence?
The range() is the built-in function to be used for iterating the sequence of numbers.
How many kinds of sequences are supported by Python? What are they?
Python supports six sequence types, they are:
- unicode strings
- range objects
Explain the shortest way to open a text file and display its contents?
How to redirect stdout to a file in Python?
You can use shell redirection while executing the python file.
How to display the contents of text file in reverse order?
The reversed() method returns an iterator that accesses the given sequence in the reverse order.
Explain what is Dogpile effect? How can you prevent this effect?
The Dogpile effect occurs when cache expires and websites are hit by numerous requests the same time. It is triggered because we allowed more than one request to execute the expensive query.
Dog pile effect can be prevented using semaphore lock. If value expired, first process acquires a lock and starts generating new value. All the subsequent requests check if lock is acquired and serve stale content. After new value is generated, lock is released.