1

This poll = ['Kassel', 33, 'Berlin', 25, 'Hamburg', 23] should convert to {'Kassel': 33, 'Berlin': 25, 'Hamburg': 23}.

This works:

location = poll[::2]
result = poll[1::2]
poll = dict(zip(location, result))

Is there a faster, more pythonic way to pair values in a list of length n?

John
  • 1,646
  • 2
  • 22
  • 30

1 Answers1

2

A dict comprehension like this would do:

{poll[i]: poll[i + 1] for i in range(0, len(poll), 2)}
deceze
  • 491,798
  • 79
  • 706
  • 853