0

In line 52 of my code I keep getting the error, fact_check.py:52: [C0206(consider-using-dict-items), get_totals] Consider iterating with .items() and I don't know how to fix it. the section of code is as follows...

def get_totals(data, presidents):
    '''
    Description: Takes a list of raw data, and a dictionary of years adn the associated president.
    It then creates an output list, with the total jobs and their associated president
    Requires: List data, Dictionary presidents
    Returns: List output
    '''
    output = []
    pre_output = {}
    for i in data:
        i[0] = int(i[0])
        try:
            pre_output[presidents[i[0] - 1]].append(i[1])
            pre_output[presidents[i[0]]].extend(i[1: ])
        except KeyError:
            pre_output[presidents[i[0]]] = []
            pre_output[presidents[i[0]]].extend(i[1: ])
    for i in pre_output:  # <---- (line 52)
        k = pre_output[i]
        tmp_list = [i]
        before = int(k[0])
        total = 0
        j = _
        for j in k:
            if j == '':
                continue
        j = int(j)
        total += j - before
        before = j
        tmp_list.append(total)
        output.append(tmp_list)
    return output

Haven't tried much because honestly I don't know why its doing this. Any information helps.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    That's not an error, and it was not issued by Python. You can replace those first two statements with `for i,k in pre_output.items():`. and get both at once. – Tim Roberts Dec 12 '22 at 21:45
  • That doesn't sound like an error, but a hint/tip to improve your code. Instead of `for i in pre_output:` and then indexing with `i`, consider using `for k, v in pre_output.items()` you can then use the key `k` and value `v` directly. – Grismar Dec 12 '22 at 21:46
  • By the way, it's better to use `if key in dict/list:` instead of using `try/except` in that case. `try/except` can be expensive. – Tim Roberts Dec 12 '22 at 21:47
  • Beside the point, but you have a duplicate line on either branch of a conditional: `pre_output[presidents[i[0]]].extend(i[1: ])`. Just put it after the try-except. – wjandrea Dec 12 '22 at 21:55

1 Answers1

0

That's not an error, and it was not issued by Python. You can replace those first two statements with for i,k in pre_output.items(): and get both at once.

-- Comment by Tim Roberts

wjandrea
  • 28,235
  • 9
  • 60
  • 81