2

Is there a way in python to perform the same function as enumerate() but with letters instead?

For example

x = ['block', 'cheese']

for i, word in enumerate(x):
    print((i, word))

would produce

(1, 'block')
(2, 'cheese')

Is there a straightforward way to produce this?

('A', 'block')
('B', 'cheese')
Adam_G
  • 7,156
  • 18
  • 76
  • 136

3 Answers3

7

For up to 26 elements you can do:

import string

x = ['block', 'cheese']

for i, word in zip(string.ascii_uppercase, x):
    print((i, word))

Output

('A', 'block')
('B', 'cheese')
DarrylG
  • 14,084
  • 2
  • 15
  • 21
2

No it's not possible.

But maybe string.ascii_lowercase could help you

import string
string.ascii_lowercase
>>>>> 'abcdefghijklmnopqrstuvwxyz'
string.ascii_lowercase[0]
>>>>> a
AlexisG
  • 2,337
  • 3
  • 8
  • 24
1

You could relatively easy mimic Excel's behaviour with a generator:

def mimic_excel():
    for i in range(0, 26):
        yield chr(i + 65)

    i, j = [0, 0]

    for j in range(0, 26):
        for i in range(0, 26):
            yield "{}{}".format(chr(j + 65), chr(i + 65))


for letter in mimic_excel():
    print(letter)

This yields

A
B
C
...
ZX
ZY
ZZ
Jan
  • 40,932
  • 8
  • 45
  • 77