pythonsetappend

Append values to a set in Python


How do I add values to an existing set?


Solution

  • your_set.update(your_sequence_of_values)
    

    e.g, your_set.update([1, 2, 3, 4]). Or, if you have to produce the values in a loop for some other reason,

    for value in ...:
        your_set.add(value)
    

    But, of course, doing it in bulk with a single .update call is faster and handier, when otherwise feasible.