Skip to content

Instantly share code, notes, and snippets.

@oprypin
Created April 11, 2026 11:57
Show Gist options
  • Select an option

  • Save oprypin/56b2d3b56487acef34a44a47c943b7f6 to your computer and use it in GitHub Desktop.

Select an option

Save oprypin/56b2d3b56487acef34a44a47c943b7f6 to your computer and use it in GitHub Desktop.
Rebrickable: Find all minifigs that can be built from an exported part list
import collections
import csv
inventory_id_to_fig_num: dict[int, str] = {}
print('Reading inventories') # From https://rebrickable.com/downloads/
with open('inventories.csv', encoding='utf-8') as f:
for row in csv.DictReader(f):
if row['set_num'].startswith('fig-'):
inventory_id_to_fig_num[int(row['id'])] = row['set_num']
minifig_inventories: dict[str, collections.Counter[tuple[str, int]]] = {}
print('Reading inventory parts') # From https://rebrickable.com/downloads/
with open('inventory_parts.csv', encoding='utf-8') as f:
for row in csv.DictReader(f):
fig_num = inventory_id_to_fig_num.get(int(row['inventory_id']))
if fig_num:
inventory = minifig_inventories.setdefault(fig_num, collections.Counter())
inventory[row['part_num'], int(row['color_id'])] += int(row['quantity'])
my_parts: collections.Counter[tuple[str, int]] = collections.Counter()
print('Reading owned parts') # From a part list that you exported
with open('rebrickable_parts_my-parts.csv', encoding='utf-8') as f:
for row in csv.DictReader(f):
my_parts[row['Part'], int(row['Color'])] += int(row['Quantity'])
print('Finding matches') # By intersecting each minifig with the entire part list
result_minifigs: list[str] = []
for fig_num, fig_inventory in minifig_inventories.items():
needed_count = fig_inventory.total()
have_count = (fig_inventory & my_parts).total()
if have_count == needed_count:
result_minifigs.append(fig_num)
print('Writing a new set list')
with open('rebrickable_sets_matching-minifigs.csv', 'w', encoding='utf-8') as f:
print('Set Number,Quantity,Includes Spares,Inventory Ver', file=f)
for fig_num in result_minifigs:
print(f'{fig_num},0,False,1', file=f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment