Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interleave list with fixed element

Tags:

python

list

I know that I can interleave two python lists with:

[elem for pair in zip(*lists) for elem in pair]

Now I need to interleave a list with a fixed element like:

list = [1, 2, 3, 4]
# 🐍 python magic 🐍
output = [1, 0, 2, 0, 3, 0, 4]
like image 817
enrico.bacis Avatar asked May 04 '15 13:05

enrico.bacis


1 Answers

One really straightforward solution is:

[elem for x in list for elem in (x, 0)][:-1]
like image 130
enrico.bacis Avatar answered Oct 13 '22 20:10

enrico.bacis