With a structure like this
hapts = [('1|2', '1|2'), ('3|4', '3|4')]
I need to zip it (sort of…) to get the following:
end = ['1|1', '2|2', '3|3', '4|4']
I started working with the following code:
zipped=[]
for i in hapts:
tete = zip(i[0][0], i[1][0])
zipped.extend(tete)
some = zip(i[0][2], i[1][2])
zipped.extend(some)
… and got it zipped like this:
zipped = [('1', '1'), ('2', '2'), ('3', '3'), ('4', '4')]
Any suggestions on how to continue? Furthermore i’m sure there should a more elegant way to do this, but is hard to pass to Google an accurate definition of the question 😉
Thx!
You are very close to solving this, I would argue the best solution here is a simple
str.join()in a list comprehension:This also has the bonus of working nicely with (potentially) more values, without modification.
If you wanted tuples (which is not what your requested output shows, as brackets don’t make a tuple, a comma does), then it is trivial to add that in:
Also note that
zippedcan be produced more effectively too:And to show what I meant before about handling more values elegantly: