코드로 우주평화

0102. Enumerable Magic #25 - Take the First N Elements 본문

나는 이렇게 학습한다/Algorithm & SQL

0102. Enumerable Magic #25 - Take the First N Elements

daco2020 2023. 1. 3. 00:23

Create a function that accepts a list/array and a number n, and returns a list/array of the first n elements from the list/array.



Solution:

def take(arr, n):
    result = []
    arrs = [arr[i-1:i] for i in range(1, n+1)]
    for arr in arrs:
        result += arr
    return result