-3

I need to print a list in Python nicely and numbered. For example, if this is my list:

list = ["hello", "dad", "milk"]

this is the output I want:

[1] -hello
[2] -dad
[3] -milk

This is what I have tried:

list = ["hello", "dad", "milk"]
list_element = 0
stuff = len(list)
while stuff != list_element:
            element = list[list_element]
            print(f"[{list_element}] -{element}")
            list_element = list_element + 1

and this is the output I get:

[0] -hello
[1] -dad
[2] -milk

but I don't know how to make it start from 1 (I know in programming you start with 0, but I want to start with 1!)

edit: I forgot to mention this was homework and we didn't learn about the for loop yet so my teacher said I shouldn't use it as to not confuse my classmates lol

  • 3
    `list_element + 1`? Also this is a much more natural fit for a `for` than `while` loop, this would be trivial if you `enumerate`d (and don't shadow `list`, use a different name). – jonrsharpe Feb 21 '22 at 18:13
  • 1
    Python's *index* starts at 0. You can set any increments to start at whatever number you'd like – Wondercricket Feb 21 '22 at 18:13
  • `list_element = 0` You're telling it to start at zero. If you want to start at 1, do that instead! – John Gordon Feb 21 '22 at 18:14
  • You can print whatever you like in the `print()` call. All that matters is that you _access_ the list element with the zero-based index. Also, the pythonic way to loop over a list is using `for element in list`. If you want the index too, do `for index, element in enumerate(list)`. Read the [docs for `enumerate()`](https://docs.python.org/3/library/functions.html#enumerate) to see if it can do what you want to do without having to add 1 to your print statement. – Pranav Hosangadi Feb 21 '22 at 18:14
  • @jonrsharpe so the loop can end – Fernandokan Feb 21 '22 at 18:15
  • Sorry, I don't mean "why do you have `list_element = list_element + 1`?", I mean "why _don't_ you have `list_element + 1` **when you print**?", if what you want is to show a number one larger than the index? – jonrsharpe Feb 21 '22 at 18:17

2 Answers2

0

You can simply to it using enumerate in Python, as follows:

list = ["hello", "dad", "milk"]

for i, element in enumerate(list, 1): # to start with index 1, instead of 0
    print(f"[{i}] -{element}")

Result:

[1] -hello
[2] -dad
[3] -milk

You can get more informatoin and example about enumerate here: https://www.programiz.com/python-programming/methods/built-in/enumerate

In additon, one tip is that it is not good to use pre-defined keyword as a variable name such as list.

Park
  • 2,446
  • 1
  • 16
  • 25
0

You could also use enumerate.

myList = ["hello", "dad", "milk"]
for count, element in enumerate(myList):
    print(f"[{count+1}] -{element}")
Supertech
  • 746
  • 1
  • 9
  • 25
  • I am curios what is the reason for a down vote for this. As I have noticed at least one more person suggested the same thing and get a similar down vote. – Supertech Feb 21 '22 at 18:27