Question: I have a list of lists like the following, and the number and length of sublists can be variable:

```
test = [[1, 5, 4, 3, 5, 2], [4, 2], [5, 2, 4, 3, 5], [5, 3, 1]]

```

I want to downsample all sublists to the length of the shortest sublist - this case 2. That means I want to randomly select 2 elements from all sublists as an output.

For a much larger list of around 100 sublists, each greater than 100000 items, what would the most efficient way be?

Answer: 
Using a [generator expression](https://docs.python.org/3/reference/expressions.html#generator-expressions) and [list comprehension](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) with [`random.sample()`](https://docs.python.org/3.5/library/random.html#random.sample) like:

### Code:

```
min_len = min(len(x) for x in data)
[random.sample(x, min_len) for x in data]

```

### Test Code:

```
import random

data = [[1, 5, 4, 3, 5, 2], [4, 2], [5, 2, 4, 3, 5], [5, 3, 1]]
min_len = min(len(x) for x in data)
print([random.sample(x, min_len) for x in data])

```

### Results:

```
[[5, 4], [4, 2], [4, 5], [5, 3]]

```
Another Way to do it:

```
import random
test = [[1, 5, 4, 3, 5, 2], [4, 2], [5, 2, 4, 3, 5], [5, 3, 1]]
minlen = min(list(map(lambda x: len(x),test)))
print([random.sample(i,minlen) for i in test])

```

Output:

```
[[3, 5], [4, 2], [5, 3], [1, 3]]

```