106
78

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonでリストの最後(一番後ろ)の値を取得する方法

Last updated at Posted at 2014-08-15

可変でいくつまでリストの中身が存在しているのかが、叩いてみるまでわからない時に使います。

リストの作成

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

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

特定のデータを取り出す

print list[0]
a

print list[2]
c

データが存在しない場合

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

一番後ろを指定する

print list[-1]
e

後ろから二番目を指定する

print list[-2]
d

知っていればなんてことはないですが、知らないと力技になっちゃいますね・・。

106
78
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
106
78

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?