0

here my_list is a list containing string(alphabets). i have to select (no. user input) of alphabets randomly. my task is to add these (no. user input) to a list and then do a little bit further operation with that list. my problem here is I am unable to add rndm to my checklist .

here is m function

for i in range(0, n_subs):    
    rndm=my_list[random.randint(0, n_subs)]
    checklist.insert(rndm)
print checklist#check
4
  • What is the error message? Commented Jan 19, 2014 at 20:34
  • 4
    Did you mean checklist.append(rndm)?
    – Fenikso
    Commented Jan 19, 2014 at 20:35
  • or perhaps checklist.insert(0,rndm) Commented Jan 19, 2014 at 20:37
  • 5
    you can always get help by typing help(checklist.insert) (assuming you want to insert and not append) Commented Jan 19, 2014 at 20:38

2 Answers 2

3

insert() needs two parameters - index and object.

If you want to append to the end of the list, just use append().

3

You need to use the append function to append values to the end of a list.

So instead of doing checklist.insert(rndm), do checklist.append(rndm).

In case you want to insert values at specific location, use checklist.insert(i, rndm), where i is the index of the element before which you want to insert.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.