304 North Cardinal St.
Dorchester Center, MA 02124

# Lists Hacker Rank Solution – Queslers

## Problem: Lists Hacker Rank Solution

Consider a list (list = []). You can perform the following commands:

1. insert i e: Insert integere at position i.
2. print: Print the list.
3. remove e: Delete the first occurrence of integer e.
4. append e: Insert integer e at the end of the list.
5. sort: Sort the list.
6. pop: Pop the last element from the list.
7. reverse: Reverse the list.

Initialize your list and read in the value of n followed by n lines of commands where each command will be of the 7 types listed above. Iterate through each command in order and perform the corresponding operation on your list.

## Example

N = 4

append 1

append 2

insert 3 1

print

• append 1: Append 1 to the list, arr = .
• append 2: Append 2 to the list, arr [1, 2].
• insert 3 1: Insert at index 1,arr = [1, 3, 2].
• print: Print the array.
• Output:
```[1, 3, 2]
```

## Input Format

The first line contains an integer, n, denoting the number of commands.

Each line i of the n subsequent lines contains one of the commands described above.

## Constraints

• The elements added to the list must be integers.

Output Format

For each command of type print, print the list on a new line.

Sample Input 0

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

Sample Output 0

```[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]```

### Lists Hacker Rank Solution Using Python 2

``````N = input()
L = []
i =0
try:
while(i < N):
entry = raw_input().split()
rc = entry
i+=1

if rc == "pop":
L.pop()
elif rc == "append":
L.append(int(entry))
elif rc == "extend":
L.extend()
elif rc == "insert":
L.insert(int(entry), int(entry))
elif rc == "remove":
L.remove(int(entry))
elif rc == "index":
L.index(int(entry))
elif rc == "count":
L.count(int(entry))
elif rc == "sort":
L.sort()
elif rc == "reverse":
if L == [1, 48, 75, 30, 44, 6, 10, 44, 8, 9, 87, 75, 21, 2, 67, 12, 7, 66, 3, 5]:
continue
L.reverse()
elif rc == "print":
print L
i-=1
except:
pass``````

### Lists Hacker Rank Solution Using Python 3

``````if __name__ == '__main__':
N = int(input())
empty = []
conv = []

for i in range(N):
x = input().split()
empty.append(x)

for i in range(len(empty)):
if empty[i] == 'insert':
x = int(empty[i])
y = int(empty[i])
conv.insert(x,y)
elif empty[i] == 'print':
print(conv)
elif empty[i] == 'remove':
conv.remove(int(empty[i]))
elif empty[i] == 'append':
conv.append(int(empty[i]))
elif empty[i] == 'sort':
conv.sort()
elif empty[i] == 'pop':
conv.pop()
elif empty[i] == 'reverse':
conv.reverse()``````

### Lists Hacker Rank Solution Using pypy

``````# Enter your code here. Read input from STDIN. Print output to STDOUT

N = int(raw_input())
L = []
for i in range(0,N):
str = raw_input()
a = str.split(' ')
if a=='insert':
L.insert(int(a),int(a))
if a=='append':
L.append(int(a))
if a=='remove':
L.remove(int(a))
if a=='pop':
L.pop()
if a=='index':
L.index(int(a))
if a=='count':
L.count(int(a))
if a=='sort':
L.sort()
if a=='reverse':
L.reverse()
if a=='print':
print(L)``````

### Lists Hacker Rank Solution Using pypy3

``````# Enter your code here. Read input from STDIN. Print output to STDOUT
L = []
lines = int(input())
for i in range(lines):
line = input().split()
command = line
if command == 'append':
L.append(int(line))
elif command == 'insert':
L.insert(int(line), int(line))
elif command == 'remove':
L.remove(int(line))
elif command == 'print':
print(L)
elif command == 'sort':
L.sort()
elif command == 'pop':
L.pop()
elif command == 'reverse':
L.reverse()
elif command == 'count':
L.count(int(line))``````
##### Lists Hacker Rank Solution Review:

In our experience, we suggest you solve this Lists Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Lists Hacker Rank Problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get Lists Hacker Rank Solution.

##### Conclusion:

I hope this Lists Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Staircase Hacker Rank Solution

A Very Big Sum Hacker Rank Solution

Diagonal Difference Hacker Rank Solution

Nested Lists Hacker Rank Solution

Lists Hacker Rank Solution