Find more articles in our new Python Knowledge Base

Using single letter to name your variables

Sometimes you see programmers trying to shorten the amount of text needed to write a piece of code, but when this goes to extremes, it will result in extremely ugly and unreadable code.

Anti-pattern

d = {'data': [{'a': 'b'}, {'b': 'c'}, {'c': 'd'}], 'texts': ['a', 'b', 'c']}

for k, v in d.iteritems():
    if k == 'data':
        for i in v:
            # Do you know what are you iterating now?
            for k2, v2 in i.iteritems():
                print(k2, v2)

Best practice

Use more verbose names for your variables for clarity

It is much better to write more text and to be much more precise about what each variable means.

data_dict = {
    'data': [{'a': 'b'}, {'b': 'c'}, {'c': 'd'}],
    'texts': ['a', 'b', 'c']
}

for key, value in data_dict.iteritems():
    if key == 'data':
        for data_item in value:
            # Do you know what are you iterating now?
            for data_key, data_value in data_item.iteritems():
                print(data_key, data_value)