I find the list comprehension syntax very useful and compact. After using it all the time, it makes me want to use it outside lists, for example I would like to change:
s="sausages hamsters"
intermediate=" and "
word1,word2=s.split()
s2=word1+intermediate+word2
into
word1,word2=s.split()
s2=word1+intermediate+word2 for word1,word2 in s.split
obviously it doesn't work. Am I missing something here or this simply cannot be done in one line?
Edit : Of course this example is stupid but in some cases I can see this saving more than one line
Edit : I just wrote the example to make it more pleasant to read but the more litteral version of my question would be:
"Is there a way to create a variable this way, in one declaration:
newVar= <expression using still undefined variables> \
<some keyword or symbols> \
<declaration of pre-cited undefined variables>
just like the list comprehension allows to do:
newList=[<expression using still undefined variables>
for <declaration of pre-cited undefined variables, as result of an iterator>]
If we can do it to build lists, is it possible also for simple single objects?"