Im unable to insert items into a list not sure why | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Im unable to insert items into a list not sure why

I have take a raw input from user n stored in object L and then I did s=L.split(",") and then I tried to insert items a=s.insert(12,13).then print list (a)

28th Jun 2017, 1:34 PM
Punith N Kothari
Punith N Kothari - avatar
15 Answers
+ 1
r u sure u r entering elements separated by comma https://code.sololearn.com/cTjSwe2Q03e2/?ref=app u can try using above code . it works fine for me
28th Jun 2017, 1:55 PM
Sandesh
Sandesh - avatar
+ 1
input function will return you a string. u have to convert that string into a list based on how u enter the data using split method eg: if u give input as 1,2,3,4,5 then by using split on the string as shown in my code will give u a list [1,2,3,4,5].
28th Jun 2017, 2:15 PM
Sandesh
Sandesh - avatar
+ 1
remember insert is a method of list object and it takes 2 arguments but u have given 3 arguments. and u were getting attribute error as insert method is not there in string object
28th Jun 2017, 2:17 PM
Sandesh
Sandesh - avatar
+ 1
at the index 2 the list is assigned
28th Jun 2017, 3:14 PM
Sandesh
Sandesh - avatar
2nd Jul 2017, 8:06 AM
Punith N Kothari
Punith N Kothari - avatar
0
you can paste the error you got so that it will be easy to identify
28th Jun 2017, 1:45 PM
Sandesh
Sandesh - avatar
0
attribute error : 'str object has no attribute 'insert'
28th Jun 2017, 1:46 PM
Punith N Kothari
Punith N Kothari - avatar
28th Jun 2017, 2:10 PM
Punith N Kothari
Punith N Kothari - avatar
0
sandesh help me understand what I'm doing wrong in my code
28th Jun 2017, 2:10 PM
Punith N Kothari
Punith N Kothari - avatar
0
I want to generate a list taking input from user and then do an insert operation on it and print the list
28th Jun 2017, 2:11 PM
Punith N Kothari
Punith N Kothari - avatar
0
so my question is what If I want to insert more than 2 items in a list using insert method?
28th Jun 2017, 2:49 PM
Punith N Kothari
Punith N Kothari - avatar
0
u can check the code that I tagged earlier. u can use slicing operator to insert multiple elements
28th Jun 2017, 3:10 PM
Sandesh
Sandesh - avatar
0
so what is l[2:2] does??
28th Jun 2017, 3:12 PM
Punith N Kothari
Punith N Kothari - avatar
0
I want my list which I gave via user input and also new list added at the end .Is it possible?
2nd Jul 2017, 7:46 AM
Punith N Kothari
Punith N Kothari - avatar
0
I finally got the answer on what I was looking for .I wanted to give some default list to be added along with user input list and this code works perfectly for me.Thank you sandesh for all your help
2nd Jul 2017, 8:07 AM
Punith N Kothari
Punith N Kothari - avatar