In python, Given a list, L, the function front_two creates a new list of length 2 out of the first two items in L as follows: In the new list, the second item becomes the first item, and the first item becomes the second. If L contains only one item, the first item in the new list should be the value None. If L is empty, both items in the new list should be value None. The function returns the new list. For example: Test Result L = ['a', 'b', 'c', 'd'] print(front_two(L)) ['b', 'a'] L = [] print(front_two(L)) [None, None] L = [5, 2] print(front_two(L)) [2, 5] L = [7] print(front_two(L)) [None, 7]
In python, Given a list, L, the function front_two creates a new list of length 2 out of the first two items in L as follows:
- In the new list, the second item becomes the first item, and the first item becomes the second.
- If L contains only one item, the first item in the new list should be the value None.
- If L is empty, both items in the new list should be value None.
- The function returns the new list.
For example:
Test | Result |
---|---|
L = ['a', 'b', 'c', 'd'] print(front_two(L)) | ['b', 'a'] |
L = [] print(front_two(L)) | [None, None] |
L = [5, 2] print(front_two(L)) | [2, 5] |
L = [7] print(front_two(L)) | [None, 7] |

Step-1: Start
Step-2: function front_two(list1)
Step-2.1: Declare a list list2
Step-2.2: if length of list1 is 1, then add element None & list1[0] in list2 and return list2
Step-2.3: Otherwise, if list, then add element list1[1] & list1[0] in list2 and return list2
Step-2.4: Otherwise, return [None, None]
Step-3: Call function front_two(['a', 'b', 'c', 'd']) and print return value
Step-4: Call function front_two([]) and print return value
Step-5: Call function front_two([5, 2) and print return value
Step-6: Call function front_two([7]) and print return value
Step-7: Stop
Step by step
Solved in 4 steps with 2 images









