Skip to content

Instantly share code, notes, and snippets.

@cascad-s
Created March 11, 2015 07:14
Show Gist options
  • Save cascad-s/a31d8982dc081d7e4d35 to your computer and use it in GitHub Desktop.
Save cascad-s/a31d8982dc081d7e4d35 to your computer and use it in GitHub Desktop.
Zip two sequences
""" Module for zipped two sequences. First - keys, last - values. If
first sequence is longer, values filled "None", else ignore this pair.
"""
import itertools
def zip_sequence(s1, s2):
try:
result = dict([i for i in itertools.zip_longest(s1, s2, fillvalue=None) if i[0]])
except TypeError:
print("One or both of the elements are non-iterable.")
return False
return result
k = 'abcd'
v = '12'
print(zip_sequence(k, v))
# >>> {'b': '2', 'a': '1', 'd': None, 'c': None}
k, v = v, k
print(zip_sequence(k, v))
# >>> {'2': 'b', '1': 'a'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment