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: Show
Output:
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.
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:
The To display them, you can just use answered Apr 23, 2015 at 0:30
Josh B.Josh B. 4045 silver badges14 bronze badges 2
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:
Output:
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.
to start with index 1
Codes above will print everthing in one line. If you want to print each element in a new line
answered Jul 22, 2020 at 9:57
alercelikalercelik 4893 silver badges10 bronze badges One-liner pretty printing of
Output:
If one want to control the output format use this (with your own formatting):
Output:
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
Desired Output
In another programming language, you might create a variable called In Python, you use
How enumerate() WorksThe
But the syntax we showed above unpacks that tuple on each iteration, so:
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.
|