python: Tips for displaying an array (list) with an index (how to find out what number an element of an array is)

Thing you want to do

In Python, when an array is long, you may not know at a glance what number you want to get. (When thinking about what column to take in a column such as a table) Even if I googled it, it didn't come out so much, so I made a note.

command

print([str(i) + ": " + x for i,x in enumerate( YOUR_LIST )])

You can do it with list comprehension + enumerate!

Example

list = ["A", "B", "C", "D"]
print([str(i) + ": " + x for i,x in enumerate(list)])

>> ['0: A', '1: B', '2: C', '3: D']

Recommended Posts

python: Tips for displaying an array (list) with an index (how to find out what number an element of an array is)
How to identify the element with the smallest number of characters in a Python list?
How to count the number of occurrences of each element in the list in Python with weight
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
A comment I saw on Google+ was, "Is there an easy way to find out what number element of a list is maximum / minimum in Python?" The first line I came up with was the code on the first line, but is the second line better?
How to write what to do when an application is first displayed in Qt for Python with Designer
[Completed version] Try to find out the number of residents in the town from the address list with Python
How to find out what kind of files are stored in S3 in Python
Find out how to divide a file with a certain number of lines evenly
[Introduction to Python] How to get the index of data with a for statement
Python error messages are concrete and easy to understand "ga" (IndexError: list index out of range with element [0])
How to convert an array to a dictionary with Python [Application]
Turn an array of strings with a for statement (Python3)
How to find out if there is an arbitrary value in "somewhere" of pandas DataFrame
I tried to find out if ReDoS is possible with Python
How to use an external editor for Python development with Grasshopper
How to utilize Python with Jw_cad (Part 1 What is external transformation)
[Python] What is pip? Explain the command list and how to use it with actual examples
How to find out the number of CPUs without using the sar command
[Python] How to put any number of standard inputs in a list
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Python] How to use the enumerate function (extract the index number and element)
[Python] How to use list 2 Reference of list value, number of elements, maximum value, minimum value
Summary of how to use Python list
[Introduction to Python] What is the method of repeating with the continue statement?
Explanation of creating an application for displaying images and drawing with Python
Tips for those who are wondering how to use is and == in Python
How to build an environment for using multiple versions of Python on Mac
[python] How to sort by the Nth Mth element of a multidimensional array
Checklist on how to avoid turning the elements of numpy's array with for
What do you like about how to convert an array (list) to a string?
How to get a list of files in the same directory with python
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
How to crop an image with Python + OpenCV
How to read an array with Python's ConfigParser
How to specify attributes with Mock of python
Speed: Add element to end of Python array
I tried to find out how to streamline the work flow with Excel x Python ②
[Python] I want to use only index when looping a list with a for statement
I tried to find out how to streamline the work flow with Excel x Python ④
I tried to find out how to streamline the work flow with Excel x Python ⑤
An easy way to pad the number with zeros depending on the number of digits [Python]
I tried to find out how to streamline the work flow with Excel x Python ①
How to check in Python if one of the elements of a list is in another list
I tried to find out how to streamline the work flow with Excel x Python ③
How to find the first element that matches your criteria in a Python list
How to write a list / dictionary type of Python3
Python: Get a list of methods for an object
Let's see how to count the number of elements in an array in some languages [Go, JavaScript, PHP, Python, Ruby, Swift]
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
[Python] What is a tuple? Explains how to use without tuples and how to use it with examples.
How to use Python Kivy ③-Linkage with WebAPI (from sending and receiving requests to displaying a list of results, transitioning multiple screens)-
How to find the optimal number of clusters in k-means
[Python] How to make a list of character strings character by character
[Python] How to make an adjacency matrix / adjacency list [Graph theory]
How to swap elements in an array in Python, and how to reverse an array.
How to shuffle a part of a Python list (at random.shuffle)
Python --Find out number of groups in the regex expression
How to make a Python package (written for an intern)
How to display a list of installable versions with pyenv
How to get a list of built-in exceptions in python