How can I organize case-insensitive text and the material following it?
<p>I'm very new to Python so it'd  be very appreciated if this could be explained as in-depth as possible.</p>

<p>If I have some text like this on a text file:</p>

<pre><code>matthew : 60 kg
MaTtHew : 5 feet
mAttheW : 20 years old
maTThEw : student
MaTTHEW : dog owner
</code></pre>

<p>How can I make a piece of code that can write something like...</p>

<pre><code>Matthew : 60 kg , 5 feet , 20 years old , student , dog owner
</code></pre>

<p>...by only gathering information from the text file?</p>

<pre><code>def test_data():
    # This is obviously the source data as a multi-line string constant.
    source = \
    """
matthew : 60 kg
MaTtHew : 5 feet
mAttheW : 20 years old
maTThEw : student
MaTTHEW : dog owner
bob : 70 kg
BoB : 6 ft
    """

    # Split on newline. This will return a list of lines like ["matthew : 60 kg", "MaTtHew : 5 feet", etc]
    return source.split("\n")

def append_pair(d, p):
    k, v = p
    if k in d:
        d[k] = d[k] + [v]
    else:
        d[k] = [v]
    return d


if __name__ == "__main__":
    # Do a list comprehension. For every line in the test data, split by ":", strip off leading/trailing whitespace,
    # and convert to lowercase. This will yield lists of lists.
    # This is mostly a list of key/value size-2-lists
    pairs = [[x.strip().lower() for x in line.split(":", 2)] for line in test_data()]
    # Filter the lists in the main list that do not have a size of 2. This will yield a list of key/value pairs like:
    # [["matthew", "60 kg"], ["matthew", "5 feet"], etc]
    cleaned_pairs = [p for p in pairs if len(p) == 2]
    # This will iterate the list of key/value pairs and send each to append_pair, which will either append to
    # an existing key, or create a new key.
    d = reduce(append_pair, cleaned_pairs, {})    
    # Now, just print out the resulting dictionary.
    for k, v in d.items():
        print("{}: {}".format(k, ", ".join(v)))
</code></pre>

python