DEV Community

Discussion on: Python challenge: can you solve this in a readable and efficient way?

Collapse
 
robertgroves profile image
Robert Groves
search_dict = {}
for i in range(0, len(search_tags), 2):
    key = search_tags[i]
    search_dict[key] = search_tags[i + 1]

# ...or the same using a list comprehension...

search_dict = {
    search_tags[i]: search_tags[i + 1]
    for i in range(0, len(search_tags), 2)
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
kwnaidoo profile image
Kevin Naidoo

Nice! This is indeed correct.