-1

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
Priyanka S
  • 47
  • 5
  • 1
    You need to post some code, show us what you have tried. – OrderAndChaos Oct 10 '16 at 07:17
  • 3
    "it should create 3 lists having same name as dictionary keys". While it's certainly possible to do this in Python it's a _really_ dumb idea. If this is a homework assignment you should seriously consider looking for a different teacher. – PM 2Ring Oct 10 '16 at 07:24
  • 2
    Suggest you read _[Why you don't want to dynamically create variables_](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html). – martineau Oct 10 '16 at 07:25

2 Answers2

3

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.

Priyanka S
  • 47
  • 5
  • Unfortunately this code results in a `TypeError: can't multiply sequence by non-int of type 'NoneType'` – martineau Oct 10 '16 at 15:56
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))

but don't. How to eval a string containing an equal symbol?

Community
  • 1
  • 1
kpie
  • 9,588
  • 5
  • 28
  • 50