-2

This is my method. I am having trouble with returning the entire dictionary

def get_col(amount):
letter = 0
value = []
values = {}

for i in range(amount):
    letter = get_column_letter(i + 1)

    [value.append(row.value) for row in ws[letter]]

    values = dict(zip(letter, [value]))
    value = []
    
    return values

I want it to output it like this:

{'A': ['ID', 'value is 1', 'value is 2', 'value is 3', 'value is 4', 'value is 5', 'value is 6']}
{'B': ['Name', 'value is 1', 'value is 2', 'value is 3', 'value is 4', 'value is 5', 'value is 6']}
{'C': ['Math', 'value is 1', 'value is 2', 'value is 3', 'value is 4', 'value is 5', 'value is 6']}

But when the return is onside the 'for' it only returns

{'A': ['ID', 'value is 1', 'value is 2', 'value is 3', 'value is 4', 'value is 5', 'value is 6']}

and when the return is outside the 'for' loop, it returns

{'C': ['Math', 'value is 1', 'value is 2', 'value is 3', 'value is 4', 'value is 5', 'value is 6']}

Any help would be appreciated. Thank you!

  • Please edit the code in your question so it is a [mre] - including some minimal data which show the problem with the output of your code. – DisappointedByUnaccountableMod May 27 '21 at 22:42
  • 2
    You overwrite the dictionary `values` on every iteration - try updating dictionary : https://stackoverflow.com/a/577241/2711811 . `values.update(dict(zip(letter,[value])))` and return "outside" loop. –  May 27 '21 at 22:42
  • It's **much** easier to use `ws.iter_cols()` to do this. – Charlie Clark May 28 '21 at 09:08

2 Answers2

1

Your desired output is not a single dictionary. It's a list of dictionaries. In the for loop, at each iteration you are creating a new dictionary. When you return, you either return the first one you create or the last one if you put the return inside or outside respectevely.

You need to return a list of the created dictionaries

def get_col(amount):
    letter = 0
    value = []
    values = {}
    
    values_list = []
    
    for i in range(amount):
        letter = get_column_letter(i + 1)

        [value.append(row.value) for row in ws[letter]]

        values = dict(zip(letter, [value]))
        value = []
        
        values_list.append(values)
        
    return values_list
dzang
  • 2,160
  • 2
  • 12
  • 21
1

I am assuming you want all of the data in one dictionary:

values = dict(zip(letter, [value]))

Currently this part of your code overites the dictionary everytime. It is why you get the "A" dict with returning before the for loop finishes, and why after the loop finishes when return the dict is only the "C" dict as the "A" and "B" were overwriten.

Put the return outside the for loop afterwards, and instead of

values = dict(zip(letter, [value]))

use

values[letter] = value

as this will append more keys/values to the dict.

ps. This is my first post, I hope it helps and is understandable.

edit: If you are wanting a list of three dictionaries like your desired output shows do this:

def get_col(amount):
  letter = 0
  value = []
  values = []

  for i in range(amount):
    letter = get_column_letter(i + 1)

    [value.append(row.value) for row in ws[letter]]

    values.append(dict(zip(letter, [value])))
    value = []
    
  return values
Byverone
  • 46
  • 6