Question: I am trying to improve my list comprehension capabilities. I would like to turn the following into a Pythonic code line:

```
# Sample sentence
sample_sentence = "Once upon a time, three people walked into a bar, Once upon a time, three people walked into a bar"

# Empty list to hold words from sentence
words = []

# Empty lists to hold longest and shortest items
longest = []
shortest = []

# Pythonic method to split sentence into list of words
words = [c for c in sample_sentence.split()]

# Non-Pythonic routine to find the shortest and longest words
for i in words:
    if len(i) <= shortest_len:
        # shortest = [] # Reset list
        shortest.append(i)
    if len(i) >= longest_len:
        longest.append(i)

print(f"Shortest item: {', '.join(shortest)}")
print(f"Longest item: {', '.join(longest)}")

```

I have tried creating a Pythonic version of the *non-Pythonic routine* to find the shortest and longest words:

```
shortest = [i for i in words if len(i) <= shortest_len: shortest.append(i) ]
print(shortest)
longest = [i for i in words if len(i) >= longest_len: longest.append(i)]
print(longest)

```

But it gets an invalid syntax error.

How can I construct the above two code lines and also is it possible to use a single Pythonic line to combine the two?

Answer: 
Don't make the mistake of thinking that everything has to be a clever one-liner. It is better to write code that you will be able to look at in the future and know exactly what it will do. In my opinion, this is a very Pythonic way to write your script. Yes there are improvements that could be made, but don't optimize if you don't have to. This script is reading two sentences, if it was reading entire books then we can make improvements.

As said in the comments, everyone has their own definition of "Pythonic" and their own formatting. Personally, I don't think you need the comments when it is very clear what your code is doing. For instance, you don't need to comment "Sample Sentence" when your variable is named `sample_sentence`.

```
sample_sentence = "Once upon a time, three people walked into a bar, Once upon a time, three people walked into a bar"

words = sample_sentence.split()
words.sort(key=len)

short_len = len(words[0])
long_len = len(words[-1])

shortest_words = [word for word in words if len(word) == short_len]
longest_words = [word for word in words if len(word) == long_len]

print(f"Shortest item: {', '.join(shortest_words)}")
print(f"Longest item: {', '.join(longest_words)}")

```
Try this for your list comprehension case to resolve the syntax error -

```
shortest = [i for i in words if len(i) <= shortest_len]
print(shortest)
longest = [i for i in words if len(i) >= longest_len]
print(longest)

```

Also instead of `words = [c for c in sample_sentence.split()]` you can use `words = sample_sentence.split()`