0

How to create a dictionary from a string composed by space separated words with i for i in range(0, n) as key in the dictionary ?

Tried this:

i = 0
map(dic[i+=1],input().split())

It didn't work.

It should output this:

dic={0:'apple',1:'grapes',2:'orange',3:'banana'}
Paras H S
  • 45
  • 1
  • 1
  • 8

3 Answers3

2
input_str = "Hello world"
result = {key: value for key, value in enumerate(input_str.split())}
print(result)

Output:

{0: 'Hello', 1: 'world'}

But you can use a list since this data structure is made for iterating over their contents and keeps order. If you want an int as key, just use enumerate(your_list).

In Python, when to use a Dictionary, List or Set?

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
1

You could use enumerate:

d = {}
for i, v in enumerate(input().split()):
    d[i] = v

Or simply:

d = dict(enumerate(input().split()))

But why do that? use a list...

Since your keys are simply (ordered!) integers, using a dict seems an overkill as to access integer-indexed values from a list is also O(1). For example, let's look at a small comparison with the 2 versions:

l = input().split()
d = dict(enumerate(l))

We have:

>>> print(l)
['apple', 'orange', 'banana']
>>> print(d)
{0: 'apple', 1: 'orange', 2: 'banana'}

Now let's see how we will grab values:

>>> l[0]
'apple'
>>> d[0]
'apple'
>>> l[2]
'banana'
>>> d[2]
'banana'

Dictionaries have a small memory overhead and so for this case using a dictionary doesn't give any advantage.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • It seems that `input` holds only values, so output from this would be `{'apples':'grapes', 'orange':'bannana'}`, hopefully input will be even so it won't crash – Piotr Kamoda Jul 23 '19 at 13:02
  • mmm I don't quite understand what you mean. Have you ran this code? – Tomerikoo Jul 23 '19 at 13:03
  • just look at his code: `i=0; map(dic[i+=1],input().split())` here `i` incremented is provided as dictionary keys and `input().split()` as values. Unless enumerate creates integer indices automatically, which I didn't know… – Piotr Kamoda Jul 23 '19 at 13:05
  • 1
    I think this answer could contribute from an explanation why a list would be better. – Piotr Kamoda Jul 23 '19 at 13:10
  • Agree. on it now – Tomerikoo Jul 23 '19 at 13:10
  • yes, a list is a better option.....but I thought that the run time for a dictionary is less than a list. – Paras H S Jul 23 '19 at 14:26
  • @ParasHS Not for searching by index. For checking existence yes. But I'm guessing you will not be doing this. And as I said actually a dictionary has a memory overhead so no gains in this case. A type of database should always match its use-case. Here it looks like a list is enough – Tomerikoo Jul 23 '19 at 14:31
  • If you need to iterate over a data structure, the most efficient is `set`, better than `list`. – Dorian Turba Jul 23 '19 at 15:26
  • That's not entirely true @DorianTurba. Both are `O(n)` so I don't think it can be much better :) – Tomerikoo Jul 23 '19 at 15:38
  • Sorry, I made a mistake. List is more efficient when iterate, set is more efficient when determining is an object is in the set https://stackoverflow.com/a/2831242/6251742 – Dorian Turba Jul 23 '19 at 15:49
  • @DorianTurba Yep, look again in my comment. It is exactly what I said. And note that in this case it doesn't matter anyway because the keys are sequenced integers. so we can just as well check if `i < len(lst)` to know if an *object* is in the list... – Tomerikoo Jul 23 '19 at 15:53
  • I made a mistake. But about an object in the list, how did you know the i ? – Dorian Turba Jul 23 '19 at 16:16
  • Our keys are ints. So lets say we have a dict. We will check `if i in d:` and that's fast because it's a dict. Now lets assume we have it as a list and we want to check if the same `i` is in the list. Because of the format ofthe data we can check `if i < len(lst)` @DorianTurba – Tomerikoo Jul 23 '19 at 16:37
  • This is not the right thing to do if you want to find something. Yes it works, but in that case with i, use a dict, if not, use a set. It's more pythonic. – Dorian Turba Jul 23 '19 at 20:36
  • This is an immpotent discussion as we don't know what are the actual uses of this data. All I said is that it is unlikely to be used for checking for existence because the keys are simply integers. Sounds like it will be used to access by index or iterated. Anyway as I said in a previous comment, choosing a database is according to the use-case and we don't know what that his here... – Tomerikoo Jul 23 '19 at 20:53
0
input_ = input("enter your fruits:").split(' ')

print (dict(zip([*range(len(input_))], input_)))
# print (dict(enumerate(input_)))

output:

enter your fruits:banana kiwi orange apple
{0: 'banana', 1: 'kiwi', 2: 'orange', 3: 'apple'}
ncica
  • 7,015
  • 1
  • 15
  • 37