How do I print out the index location of each of a python list so that it starts at 1, rather than 0. Here's an idea of what I want it to look like:
blob = ["a", "b", "c", "d", "e", "f"] for i in blob: print(???)Output:
1 a 2 b 3 c 4 d 5 eWhat I need to know is how do I get those numbers to show up alongside what I'm trying to print out? I can get a-e printed out no problem, but I can't figure out how to number the list.
asked Apr 22, 2015 at 23:56
You would need to enumerate your list. That means that for every letter, it has a corrosponding number.
Here is an example of your working code:
blob = ["a", "b", "c", "d", "e", "f"] for number, letter in enumerate(blob): print(number, letter)The enumerate function will give the variable number the position of the variable letter in your list every loop.
To display them, you can just use print(number, letter) to display them side by side.
answered Apr 23, 2015 at 0:30
Josh B.Josh B.
4045 silver badges14 bronze badges
2
for a, b in enumerate(blob, 1): print '{} {}'.format(a, b)
answered Apr 22, 2015 at 23:58
Malik BrahimiMalik Brahimi
16.1k5 gold badges36 silver badges66 bronze badges
Another solution using built-in operations:
Edit: In case you need extra space:
s1 = ['a', 'b', 'c', 'd'] for i in s1: print(s1.index(i) +1, end=' ') print(" ",i)Output:
1 a 2 b 3 c 4 danswered Apr 23, 2015 at 14:27
Niranjan M.RNiranjan M.R
3091 gold badge6 silver badges22 bronze badges
You can use this one-liner.
print(list(enumerate(blob)))to start with index 1
print(list(enumerate(blob, start=1)))Codes above will print everthing in one line. If you want to print each element in a new line
from pprint import pprint # no install required pprint(list(enumerate(blob, start=1)), depth=2)answered Jul 22, 2020 at 9:57
alercelikalercelik
4893 silver badges10 bronze badges
One-liner pretty printing of enumerate object:
myList = ['a', 'b', 'c'] print(*enumerate(myList), sep='\n')Output:
(0, 'a') (1, 'b') (2, 'c')If one want to control the output format use this (with your own formatting):
print('\n'.join(['{}-{}'.format(i, val) for i, val in (enumerate(myList))]))Output:
0-a 1-b 2-canswered Aug 30, 2020 at 14:26
itamar kanteritamar kanter
1,0313 gold badges10 silver badges19 bronze badges
You have a list of items and want to print it out as a numbered list using Python.
Starting List
fruit = ['banana', 'apple', 'pear', 'peach']Desired Output
- banana
- apple
- pear
- peach
In another programming language, you might create a variable called i or num and then loop through the list incrementing the variable value by 1 with each iteration.
In Python, you use enumerate():
fruit = ['banana', 'apple', 'pear', 'peach'] for i, item in enumerate(fruit,1): print(i, '. ' + item, sep='',end='')How enumerate() Works
The enumerate(iterable, start) function will return a sequence of tuples. If we loop through that sequence normally like this, we get a tuple on each iteration:
for t in enumerate(iterable): print(t) #t is a tupleBut the syntax we showed above unpacks that tuple on each iteration, so:
for a, b in enumerate(iterable): print(a) #first value in the tuple (the count) print(b) #second value in the tuple (the item in the original iterable)