Apy - 6 months ago 39

Python Question

Suppose I have a list a = [x, y, z], and I want to print

1. x

2. y

3. z

I can do it, but it is a bit lengthy, like

`c = 1`

for i in a:

print(str(c) + "." + i)

c += 1

Is there a shorter way of doing it? Any help is appreciated. Thanks in advance.

Answer

What about this example, where `i`

is the `index`

and `e`

would be each item from `a`

list.

```
for i,e in enumerate(a):
print (str(i + 1) + '.', e)
```

As @niemmi suggests, you could also start the index at 1:

```
for i,e in enumerate(a, start=1):
print (str(i) + '.', e)
```