Lists - HackerRank Python Basic Data Types Solution

Lists - HackerRank Python Basic Data Types Solution

Instructor-svgAl-Mamun Sarkar
Mar 23 , 2020

Lists - HackerRank Python Basic Data Types Solution. Insert, Print, Remove, append, sort, pop, reverse operation of python list. Click here to see the problem. 

Code:

if __name__ == '__main__':
    N = int(input())
    my_list = []
    for i in range(0, N):
        input_str = input()
        l = input_str.split()
        if l[0] == 'insert':
            my_list.insert(int(l[1]), int(l[2]))
        elif l[0] == 'print':
            print(my_list)
        elif l[0] == 'remove':
            my_list.remove(int(l[1]))
        elif l[0] == 'append':
            my_list.append(int(l[1]))
        elif l[0] == 'sort':
            my_list.sort()
        elif l[0] == 'pop':
            my_list.pop()
        elif l[0] == 'reverse':
            my_list.reverse()

Test Input:

12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print

Test Output:

[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]
  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram