My answer is very similar to AChampion's:
wm = [
'mendacious',
'dishonest',
'mendicant',
'beggar',
'meretricious',
'gaudy; specious; falsely attractive',
'mesmerize',
'to hypnotize',
'metamorphosis',
'change; transformation'
]
a = list(map(' : '.join, zip(*[iter(wm)] * 2)))
print(a)
This code may be a bit mysterious, so I'll try to explain what's going on.
iter(wm)
creates an iterator object from wm
. As the docs say, this is
an object representing a stream of data. Repeated calls to the
iterator’s __next__()
method (or passing it to the built-in function
next()
) return successive items in the stream.
We then duplicate the iterator and zip
it. This lets us iterate over the items from the pair of iterators in parallel. But we don't actually have two separate iterators: we have two references to the one iterator object, so what we get is successive pairs of the items in wm
. FWIW, this technique is discussed in How do you split a list into evenly sized chunks?.
We use map
to call the ' : '.join
method on each tuple of strings yielded by zip
. Finally, we use list
to convert the iterable returned by map
into a list (in Python 2 this step isn't needed since the Python 2 map
returns a list).
We can use this same technique to produce a dictionary instead of a list. A dictionary is a more useful structure for this data.
d = {k: v for k, v in zip(*[iter(wm)] * 2)}
print(d)
output
{'metamorphosis': 'change; transformation', 'mendicant': 'beggar', 'mendacious': 'dishonest', 'mesmerize': 'to hypnotize', 'meretricious': 'gaudy; specious; falsely attractive'}
And we can split the v
strings into lists, which makes it easier to get at the individual words:
d = {k: v.split('; ') for k, v in zip(*[iter(wm)] * 2)}
print(d)
output
{'mendacious': ['dishonest'], 'mesmerize': ['to hypnotize'], 'meretricious': ['gaudy', 'specious', 'falsely attractive'], 'metamorphosis': ['change', 'transformation'], 'mendicant': ['beggar']}