If you want to get random K elements from D dictionary, you just use
import random random.sample( D.items(), K )
and all you need.
From the Python documentation:
random pattern (population, k)
Returns a list of length k unique elements selected from a population sequence. Used for random sampling without replacement.
In your case
import csv import random genes_csv = csv.reader(open('genes.csv', 'rb')) genes_dict = {} for row in genes_csv: genes_dict[row[0]] = row[1:] length = raw_input('How many genes do you want? ') random_list = random.sample( genes_dict.items(), int(length) ) print random_list
No need to iterate over all dictionary keys
for key in genes_dict: random_list = random.sample(genes_dict.items(), int(length)) print random_list
note that you are not actually using the key variable inside your loop, which should warn you that something might be wrong here. Although it is not true that it "returns all possible combinations of 100 genes," it simply returns N random K lists of element genes (in your case 100), where N size of a dictionary that is far from "all combinations" (which is N!/(Nk)!k! )
source share