View Problem

Fetch the last element of a list

Given the list [Red, Green, Blue], access the last element ('Blue')
DiskEdit
python
list = ['Red', 'Green', 'Blue']
list[-1]
DiskEdit
csharp .NET 2.0
string[] items = new string[] { "Red", "Green", "Blue" };
List<string> list = new List<string>(items);
string last = list[list.Count - 1]; // "Blue"
DiskEdit
csharp .NET 3.5 & C# 3.0
// Make sure you import the System.Linq namespace.
// This is not the preferred way of finding the last element if you are using Lists.
string[] items = new string[] { "Red", "Green", "Blue" };
IEnumerable<string> list = new List<string>(items);
string last = list.Last(); // "Blue"
ExpandDiskEdit
erlang
Result = lists:last(List),
ExpandDiskEdit
erlang
Result = last(List),
ExpandDiskEdit
erlang
Result = hd(lists:reverse(List)),
ExpandDiskEdit
erlang
Result = lists:nth(length(List), List),
DiskEdit
clojure
(last '[One Two Three Four Five])

Submit a new solution for python, csharp, erlang, or clojure
There are 23 other solutions in additional languages (cpp, fantom, fsharp, groovy ...)