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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.