Stuck on PHP to Python Translation
Question: I'm attempting to translate some PHP code to Python and I'm stuck on the 4th
line in the following code (included for context):

    
    
    $table = array();
    for ($i = 0; $i < strlen($text); $i++) {
        $char = substr($text, $i, $look_forward);
        if (!isset($table[$char])) $table[$char] = array();
    }
    

If `array()` is used to create an array in PHP, what is `$table[$char] =
array()` doing? Creating a new array inside an existing array? Or is it
extending the array?

What is this accomplishing? What would be the Python equivalent to this?

`if (!isset($table[$char])) $table[$char] = array();`


Answer: Seems to me, you should use another data structure than `list` as `table`
variable. I suppose that `dict` should be nice for the purpose.

I've just made a quick try to mimic your PHP code in Python:

    
    
    table = {} # use dictionary instead of list here
    for char in text:
        if char not in table:
            table[char] = []
        # do your stuff with table[char]
        pass 
    

Also, I suggest you look into
<https://docs.python.org/3/library/collections.html#collections.defaultdict>

With the class the code could be rewritten in the following way:

    
    
    import collections
    
    table = collections.defaultdict(list)
    for char in text:
        # do your stuff with table[char], empty list is created by default
        pass
    

