How do i print a numbered list in python?

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   e

What 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.

How do i print a numbered list in python?

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

How do i print a numbered list in python?

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

How do i print a numbered list in python?

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   d

answered 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-c

answered 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

  1. banana
  2. apple
  3. pear
  4. 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 tuple

But 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)

How do I print a list of numbers in Python without brackets?

Use * to print a list without brackets. Call print(*value, sep=" ") with value as a list to unpack and print the elements of the list seperated by the zero or many characters contained in sep . To seperate each element with a comma followed by a space, set sep to ", " .

How do I print a Python listed order?

sort() method sorts the list in place and returns None . The list becomes sorted, which is why printing the list displays the expected value. However, print(x. sort()) prints the result of sort() , and sort() returns None .

How do I print a list from 1 to N in Python?

Use range() to create a list of numbers 1 to N. Call range(start, stop) to return a range object containing numbers start through stop - 1 . Call list(range_object) to return a list of this object.

How do you print a list element in Python?

Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by space use sep=”\n” or sep=”, ” respectively.