Can anyone explain this message? ValueError: All arrays must be of the same length (while building dropdown menus inside datatables)

I understand basically what the message means, but I don’t understand how it applies in this context.

At this point, I would appreciate any pointers, especially if you have tackled this error message before.

As an introduction, my app “reads” a culinary recipe as a text block. It parses the nutritional elements, like ingredients, and returns a nutritional profile. The process is accurate, but it is not perfect.

For this reason, I send those elements (ingredients, units of measurements, amounts, modifiers) to an editable datatable after initial processing for the user to edit and/or approve the data before the final analysis.

For the last couple of days, I’ve been trying to enhance this datatable by using Python fuzzywuzzy (now thefuzz?) to add additional options to the ingredients column with dropdown menus inside those cells. In other words - instead of one ingredient - there will be a dropdown for each ingredient plus its top four matches in the database.

The code was working until it came time to populate the ordered dictionaries.

Does the message mean the lists for the dropdown menus must be the same length?

That makes no sense to me. There are two dropdown lists in the datatable now.: ingredients (five options) and modifiers (three options). The lists that populate the ingredients row are generated by fuzzy logic, the one for the modifiers are static, as shown below. The rest have only one option.

Does it mean missing data causes some iterations of the dictionary to have fewer fields than others?

But I am baffled. The data in these ordered dictionaries goes through a rigorous validation process to ensure that each field has some value (even ‘None’) before it gets to this stage in the process. But missing values are still a possible cause for the error message. I don’t know what else it could mean.

I have run out of insights. Can anyone shed any light on its meaning?

I have pasted a minimal, meaningful example of the code below. It is the “template” for the ordered dictionary, populated via a for loop with single values for some row items and lists for the drop downs. Based on the video, that should not produce this error, though. Dropdowns with different list sizes are permissible.

Here is the code:

# For each list in the list of lists of dictionaries, create an ordered dictionary
necessary to populate the datatable.  In this case, the ing_lists are six ingredients
related to each other at last according to thefuzz. That part may  need some work,
but each ing_lists is precisely six ingredients. All the same size.
                for ing_lists in ing_matches:

                    tobe_edited_datatable_ordict = pd.DataFrame(OrderedDict([                    
                        ('Index', ingdex),
                        ('Amount', amount),
                        ('Unit', units),
                        ('Modifier', ['small', 'medium', 'large']),
                        # This is the only generated list.                        
                        ('Ingredient', ing_lists),                   
                        ('Ing_id', ing_id)                    

                        # 7. Place ordered dicts inside list.                    

                        # 8. Eliminate any dupes from tobe_edited_list
                        unique_recipe_data_list = list(unique_everseen(tobe_edited_list))

                    # 9. Save list in JSON file.
                    with open("./json/tobe_edited_ordered_ing_dicts.json", "w") as ordered_recipe_data:
                        json.dump(unique_recipe_data_list, ordered_recipe_data, indent=4)

                    # 10. Print out list of ordered dicts.
                    with open("./json/tobe_edited_ordered_ing_dicts.json", "r") as ordered_recipe_data:
                        ord_dd_dicts = json.load(ordered_recipe_data)

Here is an example of an ing_lists - this one for red lentils:

[('red lentils', 100), ('lentils', 90), (' california red kidney beans', 86), (' cans of hot red chili peppers', 86), (' cans of red  kidney beans added fat', 86)]

Here is a snippet of the entire Traceback message:

Hey @robertpfaff The issue is happening with pandas when you are creating a the tobe_edited_datatable_ordict dataframe. I’m guessing it is because the different items in there are not the same length (ingdex, amount, [“small” “medium”, “large”], …)

1 Like

Thanks for your feedback. It sounds like you are saying each menu item must be a drop-down menu, and each drop-down menu must have the same number of options for it to work. Is that correct?

EDIT: No. I didn’t get it. The dropdown menu templates would have to accept lists of lists per key for it to work. It is simply not possible to create the kind of dropdown menus I wanted based on the information available.

I had a brief exchange with Adam and he confirm that the datatables don’t offer that kind of flexibility yer.

I just got it, or at least I think so.

The ordered dictionary is holding the data for the entire table. I was treating it like each ordered dictionary represented one row in the datatable when one ordered dictionary holds all the information for the entire datatable . I need lists of equal lengths for each key. To make it work, I get rid of the loop and it looks something more like this:

tobe_edited_datatable_ordict = pd.DataFrame(OrderedDict([
(‘Index’, ingdex1, ingdex2, ingdex3, ingdex4, - one for each row in datatable),
(‘Amount’, amount1 amount2, amount3, etc… - for the number of rows),
(‘Unit’, units…for each one of the rows),
(‘Modifier’, [‘small’, ‘medium’, ‘large’], list for each row),
# This is the only generated list.
(‘Ingredient’, ing_lists for each ingredient),
(‘Ing_id1’, ing_id2 - through the number of ingredients (or rows which is the same)

Each line in the ordered dictionary must contain all the information for the entire datatable, not lust one row in the dtatable, with each key containing the same number of elements or lists - one or each row and thus of equal length.

I was thinking I needed to create many ordered dicts - one for each ingredient.

Am I closer to the right approach?