Totally Stuck With This One

I’m working through the Python Basics exercises on the HackInScience website and have arrived at exercise 18. Print every two letters pairs.

I”ve got as far as …

import string

letters = string.ascii_lowercase
a_to_z = [letters]

But now I have absolutely no idea where I’m supposed to go after this. I can sort of see that I need to print in slices of 2 letters, starting with aa ab bb etc but have no idea how to do it. I’ve been staring at the screen for about an hour now and have given up.

Can anyone help please?

I can think of at least a couple of different interpretations for this. Can you clarify your exact requirements? Assuming you need to select all the pairs and not just adjacent pairs, are “ad” and “da” equivalent for this task, or distinct? (i.e. are you looking for combinations, or permutations?) Are you supposed to do this “by hand” or is it allowed to utilized stdlib modules like itertools?

So here’s how it’s worded on the website.

Print every two letters pairs

Created by Julien Palard

Provide a script printing every possible pairs of two letters, only lower case, one by line, ordered alphabetically.

So your output should look like:

$ python3 solution.py
aa
ab
...
ba
bb
...
zz

Don’t write down manually the whole alphabet, use string.ascii_lowercase.

Based on @ Simmo response,
it appears that you want a nested iteration:

import string
letters = string.ascii_lowercase

for letter_1 in letters:
    for letter_2 in letters:
        print('{}{}'.format(letter_1, letter_2))

Ah, ok. Thanks very much for that.

That works fine but is a bit beyond my skill level. There’s no way I would have known that. Maybe I should look for some beginner books before jumping in too far.

You’re welcome. The book that I use that is pretty comprehensive and pretty much covers it all is Learning Python, 5th Edition. It does require a lot of effort, however. I am learning Python myself actually. I would strongly recommend it as it is very much like taking a course in Python.

for is just an iterator, meaning it steps through a series of items one at a time.
In the code, letters is a series of things. So, the first for loop gets a handle of one letter, then it proceeds to the next for iterator, where it iterates through the entire list of letters. Once having done so, it continues to the letter b. It then repeats this process for the remaining letters.

Good luck!

1 Like

Based on the description, the thing being sought is what is called a “Cartesian product”. A nested loop is definitely a straightforward way of enumerating all the items in a basic Cartesian product. But also just for your future reference, know that optimized or generalized versions of things like this may also be built in to the standard library. For instance in this case there is itertools.product available that can compute N-way products of any set with itself:

from string import ascii_lowercase
from itertools import product

for a, b in product(ascii_lowercase, repeat=2):
    print(f"{a}{b}")
1 Like

I wonder which solution is faster. Probably your f-string is faster but your iteration is slower (and I find it a bit misleading to mention “optimized” there). And I wonder about the simple print(a + b) as well…

I find it a bit misleading to mention “optimized” there

Giving possible motivations of why to choose stdlib options, I said “optimized or generalized”, which I stand by.

Which bit? I think the nested loops are fundamental to the lesson.

Paul has used str.format but maybe you find this less intimidating (using @Stefan2 's suggestion):

import string
letters = string.ascii_lowercase

for letter_1 in letters:
    for letter_2 in letters:
        print(letter_1 + letter_2)

The letters are just strings and concatenating strings is as simple as add.

To make testing less boring, for a time I made the second line:

letters = string.ascii_lowercase[:3]  # for testing just 'abc'
1 Like