1

Given the following sample data:

values=['A 1','B 2','C 3']

I want to create a dictionary where A maps to 1, B to 2, and C to 3. The following works, but there is repetition:

my_dict={value.split()[0]:value.split()[1] for value in values}

The repetition of value.split() looks ugly. Is there a way to more elegantly create the dictionary without repeating value.split()?

jpp
  • 159,742
  • 34
  • 281
  • 339
Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181

3 Answers3

4

Two ways I can think of:

>>> {k:v for k,v in (s.split() for s in values)}
{'A': '1', 'B': '2', 'C': '3'}
>>> dict(s.split() for s in values)
{'A': '1', 'B': '2', 'C': '3'}

I suggesting reading about the dict type: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict; in particular:

Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value.

as well as the introduction of dict-comprehensions in PEP 274:

The semantics of dict comprehensions can actually be demonstrated in stock Python 2.2, by passing a list comprehension to the built-in dictionary constructor:

>>> dict([(i, chr(65+i)) for i in range(4)])

is semantically equivalent to:

>>> {i : chr(65+i) for i in range(4)}
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
4

For a functional solution, you can use dict with map and str.split:

values = ['A 1', 'B 2', 'C 3']

res = dict(map(str.split, values))

{'A': '1', 'B': '2', 'C': '3'}
jpp
  • 159,742
  • 34
  • 281
  • 339
2

you can do this way pythonic:

>>> values =['A 1','B 2','C 3']
>>> dict(map(str.split, values))
{'A': '1', 'C': '3', 'B': '2'}

str.split([sep[, maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made). If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].

map(function, iterable, ...)

Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

you can see that dictionary is not in ordered as your list. Using collections.orderedDict we can retain the order of the input given.

>>> import collections
>>> values =['A 1','B 2','C 3']
>>> my_ordered_dict = collections.OrderedDict(map(str.split, values))
>>> my_ordered_dict
OrderedDict([('A', '1'), ('B', '2'), ('C', '3')])
Hackaholic
  • 19,069
  • 5
  • 54
  • 72