How to get the last (last) value in a list in Python

It is used when you do not know how many contents of the list are variable until you hit it.

Creating a list

list = ["a", "b", "c", "d", "e"]

print list
['a', 'b', 'c', 'd', 'e']

Retrieving specific data

print list[0]
a

print list[2]
c

If the data does not exist

print list[5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Specify the back

print list[-1]
e

Specify the second from the back

print list[-2]
d

If you know it, there is nothing you can do, but if you don't know it, it will be a powerful skill.

Recommended Posts

How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
How to get a list of files in the same directory with python
How to clear tuples in a list (Python)
How to get the files in the [Python] folder
Get the value of a specific key up to the specified index in the dictionary list in Python
How to retrieve the nth largest value in Python
How to get the variable name itself in python
[Python] How to output the list values in order
How to get the Python version
How to pass the execution result of a shell command in a list in Python
How to use the __call__ method in a Python class
Get the number of specific elements in a python list
Developed a library to get Kindle collection list in Python
I tried "How to get a method decorated in Python"
Extract the value closest to a value from a Python list element
Get the EDINET code list in Python
Get the value of a specific key in a list from the dictionary type in the list with Python
How to identify the element with the smallest number of characters in a Python list?
How to check in Python if one of the elements of a list is in another list
How to find the first element that matches your criteria in a Python list
How to determine the existence of a selenium element in Python
Try to get a list of breaking news threads in Python.
How to get all the possible values in a regular expression
How to check the memory size of a variable in Python
[Python] How to get the first and last days of the month
How to get a string from a command line argument in python
[Introduction to Python] How to use the in operator in a for statement?
How to check the memory size of a dictionary in Python
How to get the vertex coordinates of a feature in ArcPy
[Python] How to get a value with a key other than value with Enum
How to delete multiple specified positions (indexes) in a Python list
Recursively get the Excel list in a specific folder with python and write it to Excel.
I tried to create a Python script to get the value of a cell in Microsoft Excel
How to pass the execution result of a shell command in a list in Python (non-blocking version)
How to use the C library in Python
[Python] Get the files in a folder with Python
How to embed a variable in a python string
Get the caller of a function in Python
How to create a JSON file in Python
Make a copy of the list in Python
Get only the subclass elements in a list
[Algorithm x Python] How to use the list
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
A python amateur tries to summarize the list ②
How to remove duplicate elements in Python3 list
[Python] What is a formal argument? How to set the initial value
How to get the date and time difference in seconds with python
[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
[Linux] Command to get a list of commands executed in the past
How to format a list of dictionaries (or instances) well in Python
How to sort by specifying a column in the Python Numpy array.
Get last month in python
[Python] How to use list 1
How to develop in Python
[Python] How to remove duplicate values from the list
How to convert / restore a string with [] in python
[python] Get the list of classes defined in the module
[Python] How to expand variables in a character string