Created
March 11, 2015 07:14
-
-
Save cascad-s/a31d8982dc081d7e4d35 to your computer and use it in GitHub Desktop.
Zip two sequences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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