Python shuffle the middle letters in all the words of a sentence The purpose of the program is to shuffle only the middle letters of each word in a sentence, leaving it still readable like this: 


  Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and lsat ltteer be at the rghit pclae.


def garble(sentence):

    return ' '.join([word[0] + ''.join(random.sample([char for char in word[1:-1]],len(word[1:-1]))) + word[-1] if len(word) &gt; 1 else word for word in  sentence.split()])


I definitely think I have gone overboard putting it all in one line but I'm not sure where to separate it
The fact that Python allows fitting things on a single line and pack in comprehensions, does not mean you need to try to squash the code to be shorter hurting readability. I would probably at least expand the code into multiple lines with descriptive variable names.

Here is, though, an alternative solution based on regular expression replacement function:

from random import shuffle
import re


RE_GARBLE = re.compile(r"\b(\w)(\w+)(\w)\b")


def garble_word(match):
    first, middle, last = match.groups()

    middle = list(middle)
    shuffle(middle)

    return first + ''.join(middle) + last


def garble(sentence):
    return RE_GARBLE.sub(garble_word, sentence)