There is no method to retrieve the contents of a list, why these
indexes are always sequential number, starting at zero.
does not have "different indexes".
If you need individual indexes, you can use builtin range
:
it will also give you a sequence of numbers, starting at zero
by default.
But, if you want to show on the screen the indexes, for example, in a loop is -
the natural in Python is to use builtin enumerate
. It can be used
in a for
and will return you the index, along with the element in that index:
for indice, elemento in enumerate(lista): ...
Only if you really want to post the contents on a top line, and the elements on a bottom line, the thing complicates: you can’t naturally keep going between the top line and the bottom line in Python (nor any other language, if the output is in the terminal): Voce would essentially have to create a table in memory, generate the output of each cell of the table and print this table. in this simple example, it is easier to do a for range
to print the numbers, and another with the list elements, to print
the elements themselves.
The most natural is simply printing vertically:
print("indicet - elemento")
for indice, elemento in enumerate(lista):
print (f"{indice:<10}{elemento}")
(Here I used a printable formatting rule that
says to align the index to the left, with 10 spaces, using the :<10
inside the key)
to do horizontally, as you want, just do:
...
print(f"Índices: {list(range(len(lista))}")
print(f"Likes : {lista}")
range(0, Len(list)) ?
– epx
will list the amount but I needed to show all the indices
– tentandoserpython
Take a look at the function
enumerate
: https://docs.python.org/3/library/functions.html#enumerate– Daniel Mendes
This answers your question? How to print all the contents of a list in Python
– hkotsubo