Thursday, February 17, 2022

Removing Elements Python Codewars

Take an array and remove every second element from the array. Always keep the first element and start removing with the next element.

Example:

["Keep", "Remove", "Keep", "Remove", "Keep", ...] --> ["Keep", "Keep", "Keep", ...]

None of the arrays will be empty, so you don't have to worry about that!


def remove_every_other(my_list):
    res = []
    for i in range(len(my_list)):
        if i % 2 == 0:
            res.append(my_list[i])
    return res
def remove_every_other(my_list):
    return my_list[::2]

print(remove_every_other(['Hello', 'Goodbye', 'Hello Again']))

Training on Removing Elements | Codewars



Related Posts


No comments :

Post a Comment