title: Extracting a random sublist from a list in Python
body: <p>I have a python dictionary as follows:</p>
  
  <pre><code>{'APPLE_PROVIDERS' : ["some", "provider","can","be", "null"],
    ....
  }
  </code></pre>
  
  <p>What i want to do is get a random sublist from the list (which is a value) of a key. Not just one element, but a totally random sublist. Here is what I tried: </p>
  
  <pre><code>a_list = a_dict.get('APPLE_PROVIDERS', "")
  for item in a_list[randrange(0,len(a_list)) : randrange(0,len(a_list))]:
    ...do something.. 
  </code></pre>
  
  <p>This thing has two problems :</p>
  
  <ol>
  <li><p>If the list is empty, or if the dict lookup fails, the program fails since randrange has (0,0) as arguments, which results in an error </p></li>
  <li><p>Many times both randrange() calls generate the same number, especially when the list is small. This returns an empty list. For example a_list[5:5]</p></li>
  </ol>
  
  <p>So what is the best way to get a random sublist with above cases handled ? Also, I do not care about the ordering. Anything works. I just want a totally random sublist of either 0,1... till len(a_list) elements each time the for loop starts. </p>
  
  <p>If the list can be changed in some other data structure which can hold similar elements, that works for me too.</p>