1

How do i add a set of values in order (per inventory) from a user's input for e.g. {fruit:{1,2,3,4},{veggies:{5,6,7,8}}

Nothing i've tried so far ever worked.

Here's my code:

def Create_Inventory():

    clear()
    inventory_dictionary = {}

    count_inventory = int(input("Enter the number of Inventories: "))

    for num_inv in range(count_inventory):

        add_inventory = str(input("Enter Inventory #%d: " % (num_inv + 1)))
        inventory_dictionary[add_inventory] = set()

    for find_key in inventory_dictionary:
        count_item = int(input("\nHow many items in %s inventory: " % find_key))
        for num_item in range(count_item):
            #add_item = input("Enter item #%d: " % (num_item + 1))
            #inventory_dictionary[find_key].add(add_item)
            add_item = set(input("Enter item #%d: " % (num_item + 1)))
            inventory_dictionary[find_key].update(add_item)

My full code: https://pastebin.com/gu5DJX5K

1 Answer 1

2

Sets are unordered collections. What you want is impossible. Consider these alternatives:

  • list, ordered but permits duplicates.
  • dict.keys in Python 3.6+, where dictionaries are insertion ordered.
  • OrderedDict.keys in Python versions prior to 3.6.
  • Create a custom class, e.g. this recipe.
  • Use a 3rd party library, e.g. ordered-set.

See also: Does Python have an ordered set?

Sign up to request clarification or add additional context in comments.

1 Comment

I see. That's why. I'll check on those thank you. Have a wonderful day sir!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.