```
import zipfile
from collections import defaultdict
from pprint import pprint
import re
from glob import glob

filenames = glob("/batch3/*C*_paired_fastqc.zip")

pattern = re.compile(r"([A-Z0-9-_]+)_L\d{3}\.*")
grouped = defaultdict(list)

for filename in filenames:
    match = pattern.search(filename)
    if match:
        key = match.group(1)
        grouped[key].append(filename)

pprint(grouped)

for i, g in enumerate(sorted(grouped.keys())):
    for f in grouped[g]:
        print f

    print "--------"

```

output:

```
/batch3/0046-CL7_S7_L003_R1_001_output_paired_fastqc.zip
/batch3/0046-CL7_S7_L001_R1_001_output_paired_fastqc.zip
/batch3/0046-CL7_S7_L002_R2_001_output_paired_fastqc.zip
/batch3/0046-CL7_S7_L003_R2_001_output_paired_fastqc.zip
/batch3/0046-CL7_S7_L004_R2_001_output_paired_fastqc.zip
/batch3/0046-CL7_S7_L001_R2_001_output_paired_fastqc.zip
/batch3/0046-CL7_S7_L002_R1_001_output_paired_fastqc.zip
/batch3/0046-CL7_S7_L004_R1_001_output_paired_fastqc.zip
--------
/batch3/0047-CLI_S8_L002_R1_001_output_paired_fastqc.zip
/batch3/0047-CLI_S8_L004_R2_001_output_paired_fastqc.zip
/batch3/0047-CLI_S8_L002_R2_001_output_paired_fastqc.zip
/batch3/0047-CLI_S8_L003_R2_001_output_paired_fastqc.zip
/batch3/0047-CLI_S8_L004_R1_001_output_paired_fastqc.zip
/batch3/0047-CLI_S8_L001_R2_001_output_paired_fastqc.zip
/batch3/0047-CLI_S8_L003_R1_001_output_paired_fastqc.zip
/batch3/0047-CLI_S8_L001_R1_001_output_paired_fastqc.zip

```

However, I would like to have:

```
/batch3/0046-CL7_S7_L001_R1_001_output_paired_fastqc.zip
/batch3/0046-CL7_S7_L001_R2_001_output_paired_fastqc.zip
---------
/batch3/0046-CL7_S7_L002_R1_001_output_paired_fastqc.zip
/batch3/0046-CL7_S7_L002_R2_001_output_paired_fastqc.zip
---------
/batch3/0046-CL7_S7_L003_R1_001_output_paired_fastqc.zip
/batch3/0046-CL7_S7_L003_R2_001_output_paired_fastqc.zip
--------- 
/batch3/0046-CL7_S7_L004_R1_001_output_paired_fastqc.zip
/batch3/0046-CL7_S7_L004_R2_001_output_paired_fastqc.zip

```

Unfortunately, I can not figure out how to match the file names.