QBoard » Advanced Visualizations » Viz - Python » How do I create lists out of dictionary, having same name as dictionary keys and same size as value of that key?

How do I create lists out of dictionary, having same name as dictionary keys and same size as value of that key?

  • In Python, Suppose there is a dictionary named fruits:

    fruits={
    "apple":5,
    "orange":7,
    "mango":9
    }​

    On reading the dictionary items, it should create 3 lists having same name as dictionary keys viz. apple, orange and mango and size of theses lists should be 5, 7 and 9 respectively. The elements of these lists should be given from the user through console.

     
      August 11, 2021 1:51 PM IST
    0
  • fruits = ["Apple", "Pear", "Peach", "Banana"]
    
    fruit_dictionary = dict.fromkeys(fruits, "In stock")
    
    print(fruit_dictionary)
      December 2, 2021 2:57 PM IST
    0
  • Got a solution for this-

    fruit = {
        "apple": 5,
        "banana" : 10
    }
    for key in fruit.items():
        key = list(fruit.get(key)*[None]) 

     

    This will serve my need.

     
      August 12, 2021 1:43 PM IST
    0
  • d = { "apple":5, "orange":7, "mango":9 }
    for k in d:
        temp = []
        for k2 in range(0,d[k]):
            print "enter the next "+str(k)+":"
            temp.append(raw_input())
        exec(str(k)+"="+str(temp))
      November 20, 2021 12:18 PM IST
    0