Search code examples
pythonstringlistalphabet

Alphabet range with num and item from list


I have a list:

lst = ['cat', 'cow', 'dog']

Need to print:

A1 cat
B2 cow
C3 dog

How I can do it?


Solution

  • Here is one way.

    from string import ascii_uppercase
    lst = ['cat', 'cow', 'dog']
    
    for num, (letter, item) in enumerate(zip(ascii_uppercase, lst), 1):
        print('{0}{1} {2}'.format(letter, num, item))
    
    # A1 cat
    # B2 cow
    # C3 dog   
    

    Explanation

    • Use enumerate for a numeric counter with optional start value of 1.
    • Use string.ascii_uppercase to loop upper case alphabet.
    • zip and loop through, using str.format to format your output.