We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I took a long time to understand what exactly they were asking. Half the challenge in this question is figuring that out.
They provide you with a random number N, given by N = int(input()).
Next they want you to loop through the range of N, and THEN they will provide you with random input() PER loop. You need to record that input, check it with list commands, and then do that command on your list. * if the list command requires additional numbers, they will be provided in the input as well ( eg. insert requires what is being inserted into the list and at what index).
For example,
N = int(input()) # This is the random number provided
Lst = [] # this is your list
For i in range(N): # here is your loop
X = input() # this is the random list command that you need to perform on your lst
If x [0] = append # notice i added index, if input length is 3, then you need to specify the index ( eg.input = [‘insert’, ‘inserted number’,’inserted index’]
Append to your lst
etc
Thanks for the explanation. I'm still trying to comprehend this. 1) How do we capture each input subsequent to N, integer? How did you know that after N, there's additoinal input. For a beginner like myself. How do I even understand the beginning?
This code might not work if insert is followed by more than 2 inputs or if remove or append is followed by more than 1 input. It does work if the cases all follow that of the example.
I am new to python & your solution was easiest to undertstand. I tried it and it worked fine. But i was not able to understand this part "lis.insert(int(s[1]),int(s[2]))". They asked us to insert 5 at 0 index, but by using this command we are inserting both the integers, right? Or if it is otherwise, can you pls explain?
This line tries to strip (the whitespace in front/ at the end) and seperate the command you receive from the input.
For example, if the command is " append 1". The strip() makes it "append 1", the unnecessary space in front is taken out. Whereas function split(" ") uses space as seperator and split it into two words "append" and "1".
Lists
You are viewing a single comment's thread. Return to all comments →
i still don't understand what they are asking in question , it's messing up my mind
I took a long time to understand what exactly they were asking. Half the challenge in this question is figuring that out.
They provide you with a random number N, given by N = int(input()).
Next they want you to loop through the range of N, and THEN they will provide you with random input() PER loop. You need to record that input, check it with list commands, and then do that command on your list. * if the list command requires additional numbers, they will be provided in the input as well ( eg. insert requires what is being inserted into the list and at what index).
For example, N = int(input()) # This is the random number provided Lst = [] # this is your list For i in range(N): # here is your loop X = input() # this is the random list command that you need to perform on your lst If x [0] = append # notice i added index, if input length is 3, then you need to specify the index ( eg.input = [‘insert’, ‘inserted number’,’inserted index’] Append to your lst etc
Thanks! At least now i understand what is being asked.
Thanks, this explanation was much needed!
Hey Olivenko,
Thanks for the explanation. I'm still trying to comprehend this. 1) How do we capture each input subsequent to N, integer? How did you know that after N, there's additoinal input. For a beginner like myself. How do I even understand the beginning?
Really helpful explanation. Thanks a lot :)
ya u r right
can you plz explain if s[0]=="insert": lis.insert(int(s[1]),int(s[2])) how it works?
list.insert(position, element)
Thank you for providing simple solution brother..
please explain how it stated ??? i am new in programming i dont know
HI, what does this mean?
simply typcasting it
here he compared the value of 1st index in s with "append"
so if user type ------> append 3 , then this 'if' condition will work
and in lis.append(int(s[1])) he typecasted the 2nd input into
integer, since he's taking input in form of string .
NOTE - input() by default take input as string,
Simplest and best!
s=input().strip().split(" ") please explain this as im new into programming
This code might not work if insert is followed by more than 2 inputs or if remove or append is followed by more than 1 input. It does work if the cases all follow that of the example.
Hey @roy15_5722,
I am new to python & your solution was easiest to undertstand. I tried it and it worked fine. But i was not able to understand this part "lis.insert(int(s[1]),int(s[2]))". They asked us to insert 5 at 0 index, but by using this command we are inserting both the integers, right? Or if it is otherwise, can you pls explain?
Thank You
I prefer this one too, I improved a little bit:
Can you pls explain, what does the line
s=input().strip().split(" ")
does to the code. I am a noob and I find it hard to understand what does that line mean
This line tries to strip (the whitespace in front/ at the end) and seperate the command you receive from the input.
For example, if the command is " append 1". The strip() makes it "append 1", the unnecessary space in front is taken out. Whereas function split(" ") uses space as seperator and split it into two words "append" and "1".
**Thank You sister **
Thanks for the code
Me too. Understanding the intention of it takes me a lot of times.
Happy to hear that I have some company ;)