Python Interview Questions (Part 3)
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?
import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(0))
print(sorted_x)
output
[(0, 0), (1, 2), (2, 1), (3, 4), (4, 3)]
How to sort a dictionary by value?
import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))
print(sorted_x)
output
[(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]
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.
lst = [x**2 for x in range(10)]
print(lst)
output
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
same as :
lst = []
for x in range(10):
lst.append(x**2)
print(lst)
output
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
How to clone or copy a list?
Python 3.3+ adds list.copy() method, which should be as fast as slicing:
newlist = old_list.copy()
How to remove duplicates from a list ?
itemList = ['1', '2', '3', '3', '6', '4', '5', '6']
newList = []
[newList.append(item) for item in itemList if item not in newList]
print(newList)
output
['1', '2', '3', '6', '4', '5']
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.
for num in range (1, 5):
print (num)
How many kinds of sequences are supported by Python? What are they?
Python supports six sequence types, they are:
- strings
- unicode strings
- lists
- tuples
- buffers
- range objects
Explain the shortest way to open a text file and display its contents?
with open("index.txt") as f: s = f.read()
print (s)
How to redirect stdout to a file in Python?
You can use shell redirection while executing the python file.
python test.py > output.txt
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.
for line in reversed(list(open("index.txt"))):
print(line.rstrip())
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.
Related Topics
- Python Interview Questions (Part 2)
- What is python used for?
- Is Python interpreted, or compiled, or both?
- Explain how python is interpreted
- How do I install pip on Windows?
- How do you protect Python source code?
- What are the disadvantages of the Python?
- How would you achieve web scraping in Python?
- How to Python Script executable on Unix
- What is the difference between .py and .pyc files?
- What is __init__.py used for in Python?
- What does __name__=='__main__' in Python mean?
- What is docstring in Python?
- What is the difference between runtime and compile time?
- How to use *args and **kwargs in Python
- Purpose of "/" and "//" operator in python?
- What is the purpose pass statement in python?
- Why isn't there a switch or case statement in Python?
- How does the ternary operator work in Python?
- What is the purpose of "self" in Python
- How do you debug a program in Python?
- What are literals in python?
- What is Python's parameter passing mechanism?
- What is the process of compilation and Loading in python?
- Global and Local Variables in Python
- Is there a tool to help find bugs or perform static analysis?
- What does the 'yield' keyword do in Python?
- Comparison Operators != is not equal to in Python
- What is the difference between 'is' and '==' in python
- What is the difference between = and == in Python?
- How are the functions help() and dir() different?
- What is the python keyword "with" used for?
- Is all the memory freed when Python exits?
- Difference between Mutable and Immutable in Python
- Explain split() methods of "re" module in Python
- Accessor and Mutator methods in Python
- How to Implement an 'enum' in Python
- Important characteristics of Python Objects
- How to determine the type of instance and inheritance in Python
- How would you implement inheritance in Python?
- How is Inheritance and Overriding methods are related?
- How can you create a copy of an object in Python?
- How to avoid having class data shared among instances in Python?
- Static class variables in Python
- Difference between @staticmethod and @classmethod in Python
- How to Get a List of Class Attributes in Python
- Does Python supports interfaces like in Java or C#?
- What is used to create Unicode string in Python?
- Difference between lists and tuples in Python?
- What are differences between List and Dictionary in Python
- Different file processing modes supported by Python
- How do you append to a file in Python?
- What are the differences between the threading and multiprocessing?
- Is there any way to kill a Thread in Python?
- What is the use of lambda in Python?
- What is map, filter and reduce in python?
- Is monkey patching considered good programming practice?
- What is "typeerror: 'module' object is not callable"
- Python: TypeError: unhashable type: 'list'
- How to convert bytes to string in Python?
- What are metaclasses in Python?