Lets say mylist is list of 70 elements, I would like to random select 0,12,5 elements from mylist. I get syntax error at “rand:”
rand = [0, 12, 5]
LL=[]
for x in enumerate(mylist) if i in rand:
LL.append(x)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Why not just:
Or better:
But note that your code isn’t well defined. I think what you were attempting was:
This will work, but it’s unnecessary to iterate through the entire enumerated list unless you need to preserve the order from your original list.
Finally, if you just want to randomly select N elements from your list, random.sample is perfect for that.