1

For the following program, in the addEntry(self, dictKey, dictVal) function, I don't understand why the following line of code doesn't generate indexing error:

if hashBucket[i][0] == dictKey

self.buckets is initially a list of empty list: self.buckets = [[],[]...[]]
When addEntry is executed the first time, hashBucket is just an empty list, so I expect hashBucket[i][0] will generate an indexing error, but the program actually works, why? Thank you very much for your help.

Here is the program

class intDict(object):
    """A dictionary with integer keys"""

    def __init__(self, numBuckets):
        """Create an empty dictionary"""
        self.buckets = []
        self.numBuckets = numBuckets
        for i in range(numBuckets): 
            self.buckets.append([])

    def addEntry(self, dictKey, dictVal):
        """Assumes dictKey an int. Adds an entry"""
        hashBucket = self.buckets[dictKey%self.numBuckets]
        for i in range(len(hashBucket)):
            if hashBucket[i][0] == dictKey:
                hashBucket[i] = (dictKey, dictVal)
                return
        hashBucket.append((dictKey, dictVal))

    def getValue(self, dictKey):
        """Assumes dictKey an int. Returns entry associated with the key dictKey"""
        hashBucket = self.buckets[dictKey%self.numBumBuckets]
        for e in hashBucket:
            if e[0] == dictKay:
                return e[1]
        return None

    def __str__(self):
        result = '{'
        for b in self.buckets:
            for e in b:
                result = result + str(e[0]) + ":" + str(e[1]) + ','
        return result[:-1] + '}' # result[:-1] omits the last coma
1
  • for i in range(len(hashBucket)) becomes for i in range(0) which means that it never enters the loop, so only the hashBucket.append(...) is executed. Commented Sep 24, 2016 at 1:53

3 Answers 3

2

Since hashBucket is an empty list at first, for i in range(len(hashBucket)): is essentially for i in range(0):, meaning it never gets to the conditional if hashBucket[i][0] == dictKey on the first call to addEntry.

Sign up to request clarification or add additional context in comments.

Comments

2

when you try to loop over an empty list, nothing is going to happen, fire up a python interpeter and try this

>>> for i in range(len([])):
...     print(i)
... 
>>>

Nothing gets printed, so in the same way if hashBucket is empty then everything inside the for loop will never get executed

def addEntry(self, dictKey, dictVal):
    """Assumes dictKey an int. Adds an entry"""
    hashBucket = self.buckets[dictKey%self.numBuckets]
    for i in range(len(hashBucket)):
        # This is never executed if hashBucket is empty
        if hashBucket[i][0] == dictKey:
            hashBucket[i] = (dictKey, dictVal)
            return
    hashBucket.append((dictKey, dictVal))

Comments

0

When executed for first time hashBucket is empty. So range is empty. So this for loop doesn't do anything. 'for i in range(len(hashBucket)):' I think. Is that right?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.