Estimasi Waktu Baca: 7 menit

Pengenalan Set

Set merupakan tipe data tak berurut, karena pada set tidak dikenal yang namanya indeks. Jadi posisi setiap anggota set tidak jelas. Meskipun begitu set memiliki kelebihan yang tidak dimiliki tipe data lain yaitu set memiliki anggota yang unik (unique), yang artinya antar anggota satu dengan yang lain tidak ada yang sama (duplikat).

Lalu bagaimana jika kita memaksa untuk memasukkan data atau anggota yang bernilai sama?

Untuk menjawabnya kita simak bersama penjelasan di bawah ini.

Cara Membuat Set

Untuk membuat set kita butuh tanda kurung kurawal dengan setiap anggota di dalamnya dipisahkan dengan tanda koma.

<span class="token operator">>></span><span class="token operator">></span> angka <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> type<span class="token punctuation">(</span>angka<span class="token punctuation">)</span>
<span class="token operator"><</span><span class="token keyword">class</span> <span class="token string">'set'</span><span class="token operator">></span>
Python

Sudah disinggung di awal tadi, bahwa set bersifat unik yang memungkinkan kita untuk memiliki anggota yang berbeda nilainya, tidak ada yang sama antar setiap anggota. Jika ada anggota yang nilainya sama maka akan dipilih salah satu yang paling dulu dimasukkan, sedangkan yang satunya lagi akan diabaikan atau dihapus.

<span class="token operator">>></span><span class="token operator">></span> angka <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>
Python

Pada kode di atas kita tambahkan dua anggota masing-masing bernilai 1 dan 2. Saat dicetak ternyata kedua nilai tersebut tidak ada, hal ini dikarenakan set menghapusnya karena mendeteksi bahwa nilai 1 dan 2 sudah ada sebelumnya.

Macam Anggota Set

Sebelum masuk ke situ, saya ingin jelaskan sedikit bahwa penyebutan isi dalam set bisa kalian sebut sebagai data, item, elemen, atau anggota. Kalau saya lebih suka menyebutnya anggota.

Lanjut, anggota set dapat terdiri dari semua tipe data, kecuali tipe data list, dictionary, dan set itu sendiri. Kemudian untuk satu rangkaiannya bisa terdiri dari tipe yang sama atau bahkan campuran dari berbagai tipe. Untuk mengetahui bagaimana set bekerja dengan anggota dalam berbagai tipe kita akan ulas satu-satu di bawah ini.

Set Beranggotakan Tipe Data Numerik

Terdapat tiga tipe yang termasuk ke dalam tipe data numerik, antara lain integer, float, dan complex. Ketiganya dapat menjadi anggota set

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Set Beranggotakan bilangan bulat (integer) </span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> bulat <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span> 
<span class="token operator">>></span><span class="token operator">></span> bulat 
<span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Set Beranggotakan bilangan desimal (float) </span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> desimal <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">,</span> <span class="token number">5.0</span><span class="token punctuation">}</span> 
<span class="token operator">>></span><span class="token operator">></span> desimal 
<span class="token punctuation">{</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">,</span> <span class="token number">5.0</span><span class="token punctuation">}</span> 

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Set Beranggotakan bilangan kompleks (complex) </span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> kompleks <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1j</span><span class="token punctuation">,</span> <span class="token number">2j</span><span class="token punctuation">,</span> <span class="token number">3j</span><span class="token punctuation">,</span> <span class="token number">4j</span><span class="token punctuation">,</span> <span class="token number">5j</span><span class="token punctuation">}</span> 
<span class="token operator">>></span><span class="token operator">></span> kompleks 
<span class="token punctuation">{</span><span class="token number">1j</span><span class="token punctuation">,</span> <span class="token number">2j</span><span class="token punctuation">,</span> <span class="token number">3j</span><span class="token punctuation">,</span> <span class="token number">4j</span><span class="token punctuation">,</span> <span class="token number">5j</span><span class="token punctuation">}</span>
Python

Set Beranggotakan Tipe Data String

Tipe data string atau tipe data teks juga dapat menjadi anggota set, meskipun kita tahu bahwa tipe data ini juga termasuk ke dalam tipe data berurut

<span class="token operator">>></span><span class="token operator">></span> teks <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"Ani"</span><span class="token punctuation">,</span> <span class="token string">"Budi"</span><span class="token punctuation">,</span> <span class="token string">"Cita"</span><span class="token punctuation">,</span> <span class="token string">"Dika"</span><span class="token punctuation">,</span> <span class="token string">"Ega"</span><span class="token punctuation">}</span> 
<span class="token operator">>></span><span class="token operator">></span> teks 
<span class="token punctuation">{</span><span class="token string">'Ani'</span><span class="token punctuation">,</span> <span class="token string">'Budi'</span><span class="token punctuation">,</span> <span class="token string">'Cita'</span><span class="token punctuation">,</span> <span class="token string">'Dika'</span><span class="token punctuation">,</span> <span class="token string">'Ega'</span><span class="token punctuation">}</span>
Python

Pada anggota bertipe string, set masih berbaik hati dengan membedakan besar kecilnya huruf.

<span class="token operator">>></span><span class="token operator">></span> teks <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"A"</span><span class="token punctuation">,</span> <span class="token string">"a"</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> teks
<span class="token punctuation">{</span><span class="token string">'A'</span><span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">}</span>
Python

Set Beranggotakan Tipe Data Boolean

Tipe data boolean terdiri dari dua nilai, True dan False. Meskipun hanya terdiri dari dua nilai, tipe data ini juga dapat menjadi anggota set

<span class="token operator">>></span><span class="token operator">></span> boolean <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">}</span> 
<span class="token operator">>></span><span class="token operator">></span> boolean 
<span class="token punctuation">{</span><span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">}</span>
Python

Perlu diingat untuk tipe data boolean sebagai anggota set untuk nilai True dan False sama dengan nilai 0 dan 1. Jadi ketika kalian sejajarkan nilai True dengan 1 atau 0 dengan False maka salah satu dari nilai tersebut akan dihapus.

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># True = 1</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> himpunan <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> himpunan
<span class="token punctuation">{</span><span class="token boolean">True</span><span class="token punctuation">}</span>

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># False = 0</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> himpunan <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> himpunan
<span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span>
Python

Hal ini bisa terjadi karena pada kebanyakan bahasa pemrograman termasuk python, angka 0 merupakan perwakilan untuk nilai salah (False) dan angka 1 perwakilan dari nilai benar (True).

Sebenarnya pada python selama sesuatu tidak bernilai 0 atau none maka ia dianggap benar.

Set Beranggotakan Tipe Data Tuple

Selain string yang sudah disebutkan sebelumnya, keanggotaan set juga dapat terdiri dari tipe data berurut lain seperti tuple.

<span class="token operator">>></span><span class="token operator">></span> tuple_ <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">}</span> 
<span class="token operator">>></span><span class="token operator">></span> tuple_ 
<span class="token punctuation">{</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">}</span>
Python

Tipe Data Yang Tidak Dapat Menjadi Anggota Set

Seperti sudah dijelaskan sebelumnya bahwa tidak semua tipe data bisa menjadi anggota set, ada beberapa yang tidak diizinkan menjadi anggota set seperti list, dictionary, dan set itu sendiri.

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Set dengan anggota List</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> daftar <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
Traceback <span class="token punctuation">(</span>most recent call last<span class="token punctuation">)</span><span class="token punctuation">:</span>
  File <span class="token string">""</span><span class="token punctuation">,</span> line <span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">in</span> 
TypeError<span class="token punctuation">:</span> unhashable type<span class="token punctuation">:</span> <span class="token string">'list'</span>

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Set dengan anggota sesama Set </span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> himpunan <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
Traceback <span class="token punctuation">(</span>most recent call last<span class="token punctuation">)</span><span class="token punctuation">:</span>
  File <span class="token string">""</span><span class="token punctuation">,</span> line <span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">in</span> 
TypeError<span class="token punctuation">:</span> unhashable type<span class="token punctuation">:</span> <span class="token string">'set'</span>

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Set dengan anggota Dictionary </span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> kamus <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token string">"A"</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
Traceback <span class="token punctuation">(</span>most recent call last<span class="token punctuation">)</span><span class="token punctuation">:</span>
  File <span class="token string">""</span><span class="token punctuation">,</span> line <span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">in</span> 
TypeError<span class="token punctuation">:</span> unhashable type<span class="token punctuation">:</span> <span class="token string">'dict'</span>
Python

Cara Mengakses Set

Karena set tidak memiliki indeks yang mengakibatkan anggota set tidak jelas posisinya, maka penggunaan teknik pengindeksan secara manual dan teknik slicing tidak dapat diterapkan di sini. Namun meskipun begitu anggota tipe data ini masih bisa diakses dengan menggunakan perulangan. Hal ini karena….

Menggunakan Perulangan

Tipe data set termasuk salah satu objek iterasi. Objek iterasi sendiri merupakan objek yang dapat menghasilkan proses pengulangan, yang mana dari hal ini kita dapat menggunakan perulangan untuk mengakses anggotanya. Objek iterasi mencakup semua tipe data yang masuk ke dalam kategori struktur data, selain itu string juga termasuk objek iterasi.

<span class="token operator">>></span><span class="token operator">></span> siswa <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"Ani"</span><span class="token punctuation">,</span> <span class="token string">"Budi"</span><span class="token punctuation">,</span> <span class="token string">"Cita"</span><span class="token punctuation">,</span> <span class="token string">"Dika"</span><span class="token punctuation">,</span> <span class="token string">"Ega"</span><span class="token punctuation">}</span> 
<span class="token operator">>></span><span class="token operator">></span> <span class="token keyword">for</span> i <span class="token keyword">in</span> siswa<span class="token punctuation">:</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>  <span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> 
Ani
Budi
Cita
Dika
Ega
Python

Berikut contoh lain penggunaan perulangan untuk menampilkan tiap tipe data anggota set.

<span class="token operator">>></span><span class="token operator">></span> anggota <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1.1</span><span class="token punctuation">,</span> <span class="token number">1j</span><span class="token punctuation">,</span> <span class="token string">"1"</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> <span class="token keyword">for</span> i <span class="token keyword">in</span> anggota<span class="token punctuation">:</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>  <span class="token keyword">print</span><span class="token punctuation">(</span>type<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> 
<span class="token operator"><</span><span class="token keyword">class</span> <span class="token string">'bool'</span><span class="token operator">></span>
<span class="token operator"><</span><span class="token keyword">class</span> <span class="token string">'int'</span><span class="token operator">></span>
<span class="token operator"><</span><span class="token keyword">class</span> <span class="token string">'float'</span><span class="token operator">></span>
<span class="token operator"><</span><span class="token keyword">class</span> <span class="token string">'complex'</span><span class="token operator">></span>
<span class="token operator"><</span><span class="token keyword">class</span> <span class="token string">'str'</span><span class="token operator">></span>
<span class="token operator"><</span><span class="token keyword">class</span> <span class="token string">'tuple'</span><span class="token operator">></span>
Python

Membongkar Set

Membongkar set atau juga bisa disebut unpacking set merupakan proses untuk mengambil seluruh anggota di dalam suatu set dengan cara membagikannya ke beberapa variabel sekaligus. Sedangkan untuk proses pembuatannya dapat disebut packing.

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Proses Packing</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"Ani"</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token string">"Surabaya"</span><span class="token punctuation">}</span>
 
<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Proses Unpacking</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> absen<span class="token punctuation">,</span> nama<span class="token punctuation">,</span> umur<span class="token punctuation">,</span> asal <span class="token operator">=</span> data
<span class="token operator">>></span><span class="token operator">></span> absen
<span class="token number">1</span>
<span class="token operator">>></span><span class="token operator">></span> nama
<span class="token string">'Ani'</span>
<span class="token operator">>></span><span class="token operator">></span> umur
<span class="token number">20</span>
<span class="token operator">>></span><span class="token operator">></span> asal
<span class="token string">'Surabaya'</span>
Python

Pada kode tersebut variabel data yang merupakan set dengan anggota di dalamnya yang beragam ‘membagikan’ setiap anggotanya kepada variabel lain secara berurutan. Variabel absen mendapat data pertama, bergeser ke kanan variabel nama mendapat data kedua, dan seterusnya.

Set Bersifat Mutable

Mutable artinya data di dalam set dapat diubah, sama seperti list. Untuk membuktikan bahwa set bersifat mutable, kita tidak bisa melakukannya dengan cara pengindeksan secara manual. Kita butuh sebuah fungsi, dalam kasus ini kita coba merubah isi set dengan menambahkan satu anggota di dalamnya

<span class="token operator">>></span><span class="token operator">></span> himpunan <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> himpunan<span class="token punctuation">.</span>add<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
<span class="token operator">>></span><span class="token operator">></span> himpunan
<span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>
Python

Lawannya adalah immutable (tidak bisa diubah) seperti string dan tuple.

>>> # Immutable pada Tuple 
... siswa = ("Ani", "Budi", "Cita", "Dika", "Ega")
>>> siswa[2] = "Fani"
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'tuple' object does not support item assignment

>>> # Immutable pada String
... angka = "012345"
>>> angka[2] = "9"
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'str' object does not support item assignment

Berbagai Fungsi Penting Dalam Set

Karena bersifat mutable, berarti keanggotaan set dapat dimanipulasi. Manipulasi ini mencakup menambah anggota, menghapus, dan masih banyak lagi. Hal-hal tersebut dapat dilakukan dengan penggunaan fungsi bawaan python.

Menambah Anggota Set

Untuk menambah satu anggota set dapat menggunakan fungsi add().

<span class="token operator">>></span><span class="token operator">></span> angka <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> angka<span class="token punctuation">.</span>add<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>
Python

Sedangkan jika ingin menambahkan banyak anggota sekaligus maka gunakan fungsi update(). Fungsi update akan menambahkan atau menggabungkan set yang kita miliki dengan objek iterasi seperti string, list, tuple, dictionary, dan bahkan set itu sendiri.

<span class="token operator">>></span><span class="token operator">></span> angka <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span>

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Menambah set dengan anggota bertipe String </span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> angka<span class="token punctuation">.</span>update<span class="token punctuation">(</span><span class="token string">"123"</span><span class="token punctuation">)</span>
<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">'3'</span><span class="token punctuation">,</span> <span class="token string">'2'</span><span class="token punctuation">,</span> <span class="token string">'1'</span><span class="token punctuation">}</span>

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Menambah set dengan anggota bertipe List</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> angka<span class="token punctuation">.</span>update<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">'1'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token string">'3'</span><span class="token punctuation">,</span> <span class="token string">'2'</span><span class="token punctuation">}</span>

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Menambah set dengan anggota bertipe Tuple</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> angka<span class="token punctuation">.</span>update<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">'1'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token string">'3'</span><span class="token punctuation">,</span> <span class="token string">'2'</span><span class="token punctuation">}</span>

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Menambah set dengan anggota bertipe Set</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> angka<span class="token punctuation">.</span>update<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">'1'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token string">'3'</span><span class="token punctuation">,</span> <span class="token string">'2'</span><span class="token punctuation">}</span>

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Menambah set dengan anggota bertipe Dictionary</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> angka<span class="token punctuation">.</span>update<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token number">13</span> <span class="token punctuation">:</span> <span class="token string">"a"</span><span class="token punctuation">,</span> <span class="token number">14</span> <span class="token punctuation">:</span> <span class="token string">"b"</span><span class="token punctuation">,</span> <span class="token number">15</span> <span class="token punctuation">:</span> <span class="token string">"c"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">'1'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token string">'3'</span><span class="token punctuation">,</span> <span class="token string">'2'</span><span class="token punctuation">}</span>
Python

Walaupun anggota baru tersebut berbentuk rangkaian data, namun ketika sudah masuk menjadi anggota set, mereka akan dipisah sendiri-sendiri.

Untuk anggota string nilainya masih tetap menjadi string dan untuk tipe data dictionary, fungsi update() hanya mengambil key-nya saja.

Menghapus Anggota Set

Ada 4 fungsi untuk menghapus isi atau anggota dalam set, fungsi-fungsi tersebut antara lain remove(), discard(), pop(), clear().

<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">'1'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token string">'3'</span><span class="token punctuation">,</span> <span class="token string">'2'</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> angka<span class="token punctuation">.</span>remove<span class="token punctuation">(</span><span class="token string">'1'</span><span class="token punctuation">)</span>
<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token string">'3'</span><span class="token punctuation">,</span> <span class="token string">'2'</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> angka<span class="token punctuation">.</span>discard<span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>
<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token string">'3'</span><span class="token punctuation">,</span> <span class="token string">'2'</span><span class="token punctuation">}</span>
Python

Menggunakan fungsi pop(), fungsi ini akan menghapus anggota dari sebelah kiri ke sebelah kanan.

<span class="token operator">>></span><span class="token operator">></span> angka <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> angka<span class="token punctuation">.</span>pop<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token number">1</span>
<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> angka<span class="token punctuation">.</span>pop<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token number">2</span>
<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> angka<span class="token punctuation">.</span>pop<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token number">3</span>
<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> angka<span class="token punctuation">.</span>pop<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token number">4</span>
<span class="token operator">>></span><span class="token operator">></span> angka
<span class="token punctuation">{</span><span class="token number">5</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> angka<span class="token punctuation">.</span>pop<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token number">5</span>
<span class="token operator">>></span><span class="token operator">></span> angka
set<span class="token punctuation">(</span><span class="token punctuation">)</span>
Python

Sedangkan jika ingin menghapus semua anggota sekaligus, gunakan fungsi clear().

<span class="token operator">>></span><span class="token operator">></span> angka <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>
<span class="token operator">>></span><span class="token operator">></span> angka<span class="token punctuation">.</span>clear<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token operator">>></span><span class="token operator">></span> angka
set<span class="token punctuation">(</span><span class="token punctuation">)</span>
Python

Konversi Tipe Data Set

Python juga membuat sebuah fungsi yaitu set() untuk mengkonversi suatu objek iterasi ke dalam bentuk set.

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Fungsi set() pada List</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> nomor <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
<span class="token operator">>></span><span class="token operator">></span> set<span class="token punctuation">(</span>nomor<span class="token punctuation">)</span>
<span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Fungsi set() pada String</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> sekolah <span class="token operator">=</span> <span class="token string">"Sekolah Kopi"</span>
<span class="token operator">>></span><span class="token operator">></span> set<span class="token punctuation">(</span>sekolah<span class="token punctuation">)</span>
<span class="token punctuation">{</span><span class="token string">'o'</span><span class="token punctuation">,</span> <span class="token string">'h'</span><span class="token punctuation">,</span> <span class="token string">'l'</span><span class="token punctuation">,</span> <span class="token string">'k'</span><span class="token punctuation">,</span> <span class="token string">'e'</span><span class="token punctuation">,</span> <span class="token string">'S'</span><span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token string">'i'</span><span class="token punctuation">,</span> <span class="token string">'K'</span><span class="token punctuation">,</span> <span class="token string">' '</span><span class="token punctuation">}</span>

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Fungsi set() pada Tuple</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> siswa <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token string">"Ani"</span><span class="token punctuation">,</span> <span class="token string">"Budi"</span><span class="token punctuation">,</span> <span class="token string">"Cika"</span><span class="token punctuation">,</span> <span class="token string">"Denis"</span><span class="token punctuation">,</span> <span class="token string">"Erika"</span><span class="token punctuation">)</span>
<span class="token operator">>></span><span class="token operator">></span> set<span class="token punctuation">(</span>siswa<span class="token punctuation">)</span>
<span class="token punctuation">{</span><span class="token string">'Denis'</span><span class="token punctuation">,</span> <span class="token string">'Ani'</span><span class="token punctuation">,</span> <span class="token string">'Budi'</span><span class="token punctuation">,</span> <span class="token string">'Cika'</span><span class="token punctuation">,</span> <span class="token string">'Erika'</span><span class="token punctuation">}</span>

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Fungsi set() pada Dictionary</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> absensi <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token string">"Ani"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">:</span><span class="token string">"Budi"</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">:</span><span class="token string">"Cika"</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">:</span><span class="token string">"Denis"</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">:</span><span class="token string">"Erika"</span><span class="token punctuation">}</span> 
<span class="token operator">>></span><span class="token operator">></span> set<span class="token punctuation">(</span>absensi<span class="token punctuation">)</span>
<span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>
Python

Sedangkan jika kita tetap memaksa untuk menggunakan objek non-iterasi, maka akan menimbulkan error.

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Objek iterasi </span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> set<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token operator">>></span><span class="token operator">></span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">}</span>

<span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># Objek non-iterasi</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> set<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
Traceback <span class="token punctuation">(</span>most recent call last<span class="token punctuation">)</span><span class="token punctuation">:</span>
  File <span class="token string">""</span><span class="token punctuation">,</span> line <span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">in</span> 
TypeError<span class="token punctuation">:</span> <span class="token string">'int'</span> object <span class="token keyword">is</span> <span class="token operator">not</span> iterable
Python

Fungsi set() hanya membutuhkan satu parameter yaitu objek iterasi itu sendiri dan ini juga bersifat opsional. Dengan kata lain kita juga bisa mendeklarasikannya dengan nilai kosong.

<span class="token operator">>></span><span class="token operator">></span> himpunan <span class="token operator">=</span> set<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token operator">>></span><span class="token operator">></span> himpunan
set<span class="token punctuation">(</span><span class="token punctuation">)</span>
Python