python - List comprehension with several elements per entry -
this 1 clear:
a = ['a', 'b', 'c'] x = [v v in a]
but not sure how this:
sep = 5 # ??? y = [v + sep v in a] print y # expected ['a', 5, 'b', 5, 'c', 5]
how can write list comprehension multiple elements per source element?
i not interested in optimizations of code: please not refer [:]
operator or join
method or on lines. code needs list comprehension. alternative have @ moment 4 lines for
loop, inconvenient:
y = [] v in a: y.append(v) y.append(sep)
using nested list comprehension:
>>> = ['a','b','c'] >>> [item x in item in (x, 5)] ['a', 5, 'b', 5, 'c', 5]
Comments
Post a Comment