Python: copying a repeating dictionary into a list - last dict item being duped to all list items
Question: Considering the following:

    
    
    import re
    sequence = 'FFFFFF{7}FFFFFF'
    patterns = [ ('([0-9a-fA-F]+)', 'Sequence'),
        ('(\\([0-9a-fA-F]+\\|[0-9a-fA-F]+\\))', 'Option'),
        ('({[0-9a-fA-F]+})', 'Range'),
        ('(\\[[0-9a-fA-F]+:([0-9a-fA-F]+|\*)\\])', 'Slice'),
        ('(\\?\\?)+', 'Byte_value_Wildcard'),
        ('(\\*)+', 'Byte_length_wildcard') ]
    fragment_counter = 0
    fragment_dict= {}
    fragments_list = []
    while sequence:
        found = False
        for pattern, name in patterns:
            m = re.match (pattern,sequence)
            if m:
                fragment_counter+=1
                m = m.groups () [0]
                fragment_dict["index"]=fragment_counter
                fragment_dict["fragment_type"]=name
                fragment_dict["value"]=m
                print fragment_dict
                fragments_list.append(fragment_dict)
                sequence = sequence [len (m):]
                found = True
                break
         if not found: raise Exception ('Unrecognized sequence')
    
    print fragments_list
    

Each time it hits the "print fragment_dict"line, I get the correct (expected)
output:

    
    
    {'index': 1, 'fragment_type': 'Sequence', 'value': 'FFFFFF'}
    {'index': 2, 'fragment_type': 'Range', 'value': '{7}'}
    {'index': 3, 'fragment_type': 'Sequence', 'value': 'FFFFFF'}
    

however, the list item `fragments_list` is 3 copies of the final dict, not
each of the lines as I expect:

    
    
    [{'index': 3, 'fragment_type': 'Sequence', 'value': 'FFFFFF'}, {'index': 3, 'fragment_type': 'Sequence', 'value': 'FFFFFF'}, {'index': 3, 'fragment_type': 'Sequence', 'value': 'FFFFFF'}]
    

I am assuming this is because the `append` is referencing the instance of the
dict, not copying each iteration of the dict. I looked at using the `list()`
function, but on the dict item it just gives me a list of the dict keys.

What am I doing wrong?  
I'm not wedded to data type, I just needed a way to hold 3 data elements (and
maybe a 4th) for each fragment I find.


Answer: You're very close. Instead of the `list()` function, which generates a new
`list` from any sequence (in this case, a `dict` as a sequence is a sequence
of its keys), use the `dict()` function, which generates a new `dict` from any
mapping.

Or, maybe more simply, just use the `copy` method.

Or, even more simply, just move the `fragment_dict= {}` into the loop, so you
build up a new `dict` instead of continuing to reuse the same one.

