front_piece

Given an int list of any length, return a new list of its first 2 elements. If the list is smaller than length 2, use whatever elements are present.

This exercise was taken from codingbat.com and has been adapted for the Python language. There are many great programming exercises there, but the majority are created for Java.

Test 1

Input:

[1, 2, 3]

Output:

[1, 2]

Test 2

Input:

[1, 2]

Output:

[1, 2]

Test 3

Input:

[1]

Output:

[1]

Test 4

Input:

[]

Output:

[]

Test 5

Input:

[6, 5, 0]

Output:

[6, 5]

Test 6

Input:

[6, 5]

Output:

[6, 5]

Test 7

Input:

[3, 1, 4, 1, 5]

Output:

[3, 1]

Test 8

Input:

[6]

Output:

[6]