|
INTERVIEW QUESTIONS
WEB
PYTHON
DETAILS
Question: How are dictionaries implemented?
Answer: Python's dictionaries are implemented as resizable hash tables. Compared to B-trees, this gives better performance for lookup (the most common operation by far) under most circumstances, and the implementation is simpler.
Dictionaries work by computing a hash code for each key stored in the dictionary using the hash() built-in function. The hash code varies widely depending on the key; for example, "Python" hashes to -539294296 while "python", a string that differs by a single bit, hashes to 1142331976. The hash code is then used to calculate a location in an internal array where the value will be stored. Assuming that you're storing keys that all have different hash values, this means that dictionaries take constant time -- O(1), in computer science notation -- to retrieve a key. It also means that no sorted order of the keys is maintained, and traversing the array as the .keys() and .items() do will output the dictionary's content in some arbitrary jumbled order.
|
|
|
Category |
Python Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 8594 users |
Added on |
8/28/2014 |
Views |
70093 |
Rate it! |
|
|
Question:
How are dictionaries implemented?
Answer:
Python's dictionaries are implemented as resizable hash tables. Compared to B-trees, this gives better performance for lookup (the most common operation by far) under most circumstances, and the implementation is simpler.
Dictionaries work by computing a hash code for each key stored in the dictionary using the hash() built-in function. The hash code varies widely depending on the key; for example, "Python" hashes to -539294296 while "python", a string that differs by a single bit, hashes to 1142331976. The hash code is then used to calculate a location in an internal array where the value will be stored. Assuming that you're storing keys that all have different hash values, this means that dictionaries take constant time -- O(1), in computer science notation -- to retrieve a key. It also means that no sorted order of the keys is maintained, and traversing the array as the .keys() and .items() do will output the dictionary's content in some arbitrary jumbled order. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
Why are Python strings immutable?
|
View Answer
|
|
How stable is Python?
|
View Answer
|
|
Why is it called Python?
|
View Answer
|
|
Where in the world is www.python.org located?
|
View Answer
|
|
How do I submit bug reports and patches for Python?
|
View Answer
|
|
How do I get a beta test version of Python?
|
View Answer
|
|
What is Python good for?
|
View Answer
|
|
How can I overload constructors (or methods) in Python?
|
View Answer
|
|
I want to do a complicated sort: can you do a Schwartzman Transform in Python?
|
View Answer
|
|
How do I create a multidimensional list?
|
View Answer
|
|
How do you make an array in Python?
|
View Answer
|
|
How do you remove duplicates from a list?
|
View Answer
|
|
How can I find the methods or attributes of an object?
|
View Answer
|
|
How do I copy an object in Python?
|
View Answer
|
|
How do you make a higher order function in Python?
|
View Answer
|
|
How can I pass optional or keyword parameters from one function to another?
|
View Answer
|
|
How do I share global variables across modules?
|
View Answer
|
|
What are the rules for local and global variables in Python?
|
View Answer
|
|
How do you set a global variable in a function?
|
View Answer
|
|
Is there a tool to help find bugs or perform static analysis?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Python Interview Questions & Answers - Exam Mode /
Learning Mode
|