[Solved] Inserting data in a Dictionary with raw_input dynamically?


If what you want is to split the string at the first space, then you can use the string.split(s, maxsplit=n) where s is the string to split by and maxsplit=n is the number of splits to stop at. If you do not give any value for s that is only call the function as string.split(maxsplit=n) then it would split by all whitespaces.

Example –

>>> s = "Alex 45 26 35"
>>> s.split(maxsplit=1)
['Alex', '45 26 35']

You can use this to split the strings and then use first element as key and second as value in your dictionary (if that is what you really want).

You can use similar logic in your Actions case as well, split the input at first space, and then the first element of the return of split would be the action, and the second element would be the data .

To create dictionary from the list, you can use multiple methods, like –

 d= dict([s.split(maxsplit=1)])
 d
 >>> {'Alex': '45 26 35'}

Or

d = {}
slst = s.split(maxsplit=1)
d[slst[0]] = d[slst[1]]

2

solved Inserting data in a Dictionary with raw_input dynamically?