[Python] Better to add item to a set, or convert final list to a set?

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have some data that looks something like this:

    ID1 ID2 ID3
    ID1 ID4 ID5
    ID3 ID5 ID7 ID6
    ...
    ...


    where each row is a group.

    My goal is to have a dictionary for each ID, followed by a set of the other IDs that share >= 1 group with it.

    For example, this data would return {ID1: [ID2, ID3, ID4, ID5], ID2:[ID1, ID3] ... }

    I can think of 3 options for this, and I'm wondering which is (generally) best:

    1. Check whether an ID is already in the list before adding it
    2. Create sets instead of lists, and add each ID to the set
    3. Add all IDs to the list, then convert all of the lists to sets at the end.

    Continue reading...

Compartilhe esta Página