Ad

How To Append Multiple Values In The Same Key In Python 3?

Unlike the examples i've seen, I think mine is a bit different and so far haven't found anything that could help me out. So here I am again seeking for help. Been working on this for about 3 days now and i'm kind of new in Python 3 so bear with me pls. Thank you.

So far i'm getting a dictionary something like this:

{0: 'fruits', 1: 'veggies', 2: 'drinks'}

where i hope it's something like this:

{'fruits' : { 'apple', 'orange'}, 'veggies' : { 'cucumber','eggplant'}, 'drinks' : {'coke','juice'}}

and i've been trying to append additional (or multiple) values to the same key but nothing is working. It seems like it's hard to append value to a key in a dictionary. While i keep trying on doing it, i might as well seek help online.

This is my code:

# MODULES
import os

# FUNCTIONS


def clear():
    os.system('cls')


def skip():
    input("<Press Enter To Continue>")


def createINV():
    clear()
    invdict = {}
    invname = {}

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

    for a in range(countinv):

        # ADD LISTS HERE
        addinv = str(input("Enter Inventory #%d: " % (a+1)))
        invdict[a] = addinv

        print(invdict)
    for b in range(countinv):
        countitem = int(input("\nHow many items in %r inventory: " % list(invdict.values())[b]))
        for c in range(countitem):
            additem = input("Enter item #%d: " % (c+1))
            #invdict[c].extend
            #list(invdict.keys(c)[]).append(additem)
            #invdict.setdefault(c, []).append(c[additem])
            #invdict[c].append(additem)
            # d.setdefault(year, []).append(value)
    for aprint in range(countinv):
        for x,y in invdict.items():
            print (x,y)
            # for bprint in range(countitem):
            #     for y invname.value[bprint]:
            #         print(y)    


# START - Welcome
clear()
print("Hi! Welcome to Python Inventory System.")
skip()
clear()

# START - Introduction
print("This is an Inventory System where you can input any Inventoriesyou want and how many you want.")
print("For e.g.: You can input 3 types of Inventories such as Vegetables, Fast Foods, Drinks, etc.")
print("And you can input the prices for each item.")
skip()
clear()


# COMMENCE PROGRAM
x = 0
while x != 1:
    start = input("Are you ready to Start? (Y/N):")
    if start == 'y' or start == 'Y':
        x += 1
        createINV()

    elif start == 'n' or start == 'N':
        x += 1
        clear()

    else:
        x = 0
Ad

Answer

To get what you want you should modify function createINV(). To be exact you should modify the way you store the data.

def createINV():
    clear()

    # one dictionary to store all the data
    # keys are inventories
    # values are sets of items
    invdict = dict()

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

    for a in range(countinv):
        addinv = input("Enter Inventory #%d: " % (a+1))
        # initialize key-value pairs
        invdict[addinv] = set()

    # iterate over inventories
    for inv in invdict:
        countitem = int(input("\nHow many items in %s inventory: " % inv))
        for c in range(countitem):
            additem = input("Enter item #%d: " % (c+1))
            # add item to appropriate inventory
            invdict[inv].add(additem)

    print('\nYour inventory:')
    print(invdict)

Output:

Enter the number of Inventories: 3
Enter Inventory #1: qwe
Enter Inventory #2: asd
Enter Inventory #3: zxc

How many items in qwe inventory: 1
Enter item #1: rty

How many items in asd inventory: 2
Enter item #1: fgh
Enter item #2: jkl

How many items in zxc inventory: 3
Enter item #1: vbn
Enter item #2: m,.
Enter item #3: ///

Your inventory:
{'qwe': {'rty'}, 'asd': {'jkl', 'fgh'}, 'zxc': {'vbn', 'm,.', '///'}}
Ad
source: stackoverflow.com
Ad