{"id":1621,"date":"2025-09-29T22:06:12","date_gmt":"2025-09-29T15:06:12","guid":{"rendered":"https:\/\/kienthucmo.com\/?p=1621"},"modified":"2025-11-05T23:43:48","modified_gmt":"2025-11-05T16:43:48","slug":"tong-hop-cac-ham-xu-ly-set-trong-python","status":"publish","type":"post","link":"https:\/\/kienthucmo.com\/vi\/tong-hop-cac-ham-xu-ly-set-trong-python\/","title":{"rendered":"T\u1ed5ng h\u1ee3p c\u00e1c h\u00e0m x\u1eed l\u00fd Set trong Python"},"content":{"rendered":"\n<p>Set l\u00e0 m\u1ed9t trong nh\u1eefng c\u1ea5u tr\u00fac d\u1eef li\u1ec7u r\u1ea5t h\u1eefu \u00edch trong Python: n\u00f3 l\u01b0u tr\u1eef <strong>c\u00e1c ph\u1ea7n t\u1eed kh\u00f4ng tr\u00f9ng l\u1eb7p<\/strong> v\u00e0 <strong>kh\u00f4ng c\u00f3 th\u1ee9 t\u1ef1<\/strong>. Nh\u1edd \u0111\u00f3 set th\u01b0\u1eddng d\u00f9ng \u0111\u1ec3 lo\u1ea1i b\u1ecf b\u1ea3n sao, ki\u1ec3m tra thu\u1ed9c t\u00ednh (membership) r\u1ea5t nhanh, v\u00e0 th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p to\u00e1n t\u1eadp h\u1ee3p (h\u1ee3p, giao, hi\u1ec7u) m\u1ed9t c\u00e1ch tr\u1ef1c ti\u1ebfp.<\/p>\n\n\n\n<p>Trong b\u00e0i n\u00e0y m\u00ecnh s\u1ebd \u0111i t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao: c\u00e1ch t\u1ea1o set, th\u00eam\/x\u00f3a ph\u1ea7n t\u1eed, c\u00e1c ph\u00e9p to\u00e1n t\u1eadp h\u1ee3p (v\u1edbi c\u1ea3 ph\u01b0\u01a1ng th\u1ee9c v\u00e0 to\u00e1n t\u1eed), c\u00e1c ph\u01b0\u01a1ng th\u1ee9c in-place, hi\u1ec7u n\u0103ng\/\u0111\u1ed9 ph\u1ee9c t\u1ea1p, frozenset (phi\u00ean b\u1ea3n b\u1ea5t bi\u1ebfn), nh\u1eefng l\u01b0u \u00fd hay l\u1ed7i th\u01b0\u1eddng g\u1eb7p&#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">B\u1ea3ng cheat-sheet \u2014 c\u00e1c ph\u01b0\u01a1ng th\u1ee9c v\u00e0 to\u00e1n t\u1eed quan tr\u1ecdng<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Ph\u01b0\u01a1ng th\u1ee9c \/ To\u00e1n t\u1eed<\/th><th>Ch\u1ee9c n\u0103ng<\/th><th>Tr\u1ea3 v\u1ec1 \/ In-place<\/th><th>Complexity (avg)<\/th><\/tr><\/thead><tbody><tr><td><code>s.add(x)<\/code><\/td><td>Th\u00eam ph\u1ea7n t\u1eed x<\/td><td>In-place<\/td><td>O(1)<\/td><\/tr><tr><td><code>s.update(iter)<\/code><\/td><td>Th\u00eam nhi\u1ec1u ph\u1ea7n t\u1eed<\/td><td>In-place<\/td><td>O(k)<\/td><\/tr><tr><td><code>s.remove(x)<\/code><\/td><td>X\u00f3a x, KeyError n\u1ebfu kh\u00f4ng c\u00f3<\/td><td>In-place (exception)<\/td><td>O(1)<\/td><\/tr><tr><td><code>s.discard(x)<\/code><\/td><td>X\u00f3a x n\u1ebfu c\u00f3, kh\u00f4ng l\u1ed7i<\/td><td>In-place<\/td><td>O(1)<\/td><\/tr><tr><td><code>s.pop()<\/code><\/td><td>X\u00f3a v\u00e0 tr\u1ea3 v\u1ec1 1 ph\u1ea7n t\u1eed b\u1ea5t k\u1ef3<\/td><td>In-place<\/td><td>O(1)<\/td><\/tr><tr><td><code>s.clear()<\/code><\/td><td>X\u00f3a to\u00e0n b\u1ed9<\/td><td>In-place<\/td><td>O(1)<\/td><\/tr><tr><td><code>A.union(B)<\/code> \/ `A<\/td><td>B`<\/td><td>H\u1ee3p<\/td><td>New set<\/td><\/tr><tr><td><code>A.intersection(B)<\/code> \/ <code>A &amp; B<\/code><\/td><td>Giao<\/td><td>New set<\/td><td>O(min(len(A),len(B)))<\/td><\/tr><tr><td><code>A.difference(B)<\/code> \/ <code>A - B<\/code><\/td><td>Hi\u1ec7u<\/td><td>New set<\/td><td>O(len(A))<\/td><\/tr><tr><td><code>A.symmetric_difference(B)<\/code> \/ <code>A ^ B<\/code><\/td><td>Hi\u1ec7u \u0111\u1ed1i x\u1ee9ng<\/td><td>New set<\/td><td>O(len(A)+len(B))<\/td><\/tr><tr><td><code>A.update(B)<\/code> \/ `A<\/td><td>= B`<\/td><td>H\u1ee3p in-place<\/td><td>In-place<\/td><\/tr><tr><td><code>A.intersection_update(B)<\/code> \/ <code>A &amp;= B<\/code><\/td><td>Giao in-place<\/td><td>In-place<\/td><td>O(min(len(A),len(B)))<\/td><\/tr><tr><td><code>s.copy()<\/code><\/td><td>Sao ch\u00e9p shallow<\/td><td>New set<\/td><td>O(len(s))<\/td><\/tr><tr><td><code>frozenset(iter)<\/code><\/td><td>Set b\u1ea5t bi\u1ebfn<\/td><td>New frozenset<\/td><td>O(n)<\/td><\/tr><tr><td><code>x in s<\/code><\/td><td>Membership test<\/td><td>Bool<\/td><td>O(1)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">1. T\u1ea1o Set (kh\u1edfi t\u1ea1o)<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\u00e1ch c\u01a1 b\u1ea3n: d\u00f9ng <code>{}<\/code> v\u1edbi ph\u1ea7n t\u1eed ho\u1eb7c h\u00e0m <code>set()<\/code> cho t\u1eadp r\u1ed7ng.<\/li>\n\n\n\n<li><strong>Quan tr\u1ecdng:<\/strong> <code>{}<\/code> t\u1ea1o <strong>dict r\u1ed7ng<\/strong>, do \u0111\u00f3 \u0111\u1ec3 t\u1ea1o set r\u1ed7ng ph\u1ea3i d\u00f9ng <code>set()<\/code>.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Create set from literal\nnumbers = {1, 2, 3, 4, 5}\n\n# Create an empty set (note: {} is empty dict)\nempty_set = set()\n\n# Create set from iterable (list, tuple, generator)\nitems_from_list = set(&#91;1, 2, 2, 3&#93;)  # duplicates removed\nprint(items_from_list)  # Output: {1, 2, 3}\n\n# Set comprehension (concise &amp; expressive)\nsquares = {x*x for x in range(6)}  # {0, 1, 4, 9, 16, 25}\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Create set from literal<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Create an empty set (note: {} is empty dict)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">empty_set <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">set<\/span><span style=\"color: #ECEFF4\">()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Create set from iterable (list, tuple, generator)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">items_from_list <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">set<\/span><span style=\"color: #ECEFF4\">(&#91;<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">&#93;)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># duplicates removed<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">items_from_list<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># Output: {1, 2, 3}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Set comprehension (concise &amp; expressive)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">squares <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #D8DEE9FF\">x<\/span><span style=\"color: #81A1C1\">*<\/span><span style=\"color: #D8DEE9FF\">x <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> x <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">range<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">6<\/span><span style=\"color: #ECEFF4\">)}<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># {0, 1, 4, 9, 16, 25}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Gi\u1ea3i th\u00edch:<\/strong> khi chuy\u1ec3n t\u1eeb iterable -&gt; set, c\u00e1c ph\u1ea7n t\u1eed tr\u00f9ng s\u1ebd b\u1ecb lo\u1ea1i b\u1ecf. Set comprehension t\u01b0\u01a1ng t\u1ef1 list comprehension nh\u01b0ng tr\u1ea3 v\u1ec1 set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Y\u00eau c\u1ea7u v\u1ec1 ph\u1ea7n t\u1eed: <em>hashable<\/em> vs <em>unhashable<\/em><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\u00e1c ph\u1ea7n t\u1eed c\u1ee7a set <strong>ph\u1ea3i l\u00e0 hashable<\/strong> (c\u00f3 <code>__hash__<\/code> h\u1ee3p l\u1ec7): s\u1ed1, chu\u1ed7i, tuple (n\u1ebfu c\u00e1c ph\u1ea7n t\u1eed trong tuple c\u0169ng hashable).<\/li>\n\n\n\n<li>Kh\u00f4ng th\u1ec3 th\u00eam list ho\u1eb7c dict v\u00ec ch\u00fang mutable v\u00e0 kh\u00f4ng hashable.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Allowed\ns = { (1, 2), \"hello\", 42 }\n\n# Not allowed -> TypeError\n# bad = {&#91;1,2&#93;, 3}  # list is unhashable\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Allowed<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">s <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">),<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">hello<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">42<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Not allowed -&gt; TypeError<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># bad = {&#91;1,2&#93;, 3}  # list is unhashable<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Gi\u1ea3i th\u00edch:<\/strong> set d\u1ef1a tr\u00ean hash table n\u1ed9i b\u1ed9, n\u00ean m\u1ecdi ph\u1ea7n t\u1eed c\u1ea7n c\u00f3 gi\u00e1 b\u0103m c\u1ed1 \u0111\u1ecbnh trong su\u1ed1t v\u00f2ng \u0111\u1eddi trong set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Th\u00eam v\u00e0 m\u1edf r\u1ed9ng set<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>add(x)<\/code> th\u00eam m\u1ed9t ph\u1ea7n t\u1eed (n\u1ebfu ch\u01b0a t\u1ed3n t\u1ea1i).<\/li>\n\n\n\n<li><code>update(iterable)<\/code> th\u00eam nhi\u1ec1u ph\u1ea7n t\u1eed t\u1eeb iterable (list\/tuple\/set\/generator).<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>fruits = {\"apple\", \"banana\"}\nfruits.add(\"orange\")        # add single item\nfruits.update(&#91;\"mango\", \"grape\"&#93;)  # add multiple\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">fruits <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">apple<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">banana<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">fruits<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">add<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">orange<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #616E88\"># add single item<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">fruits<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">update<\/span><span style=\"color: #ECEFF4\">(&#91;<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">mango<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">grape<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">&#93;)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># add multiple<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>L\u01b0u \u00fd v\u1ec1 hi\u1ec7u n\u0103ng:<\/strong> c\u1ea3 <code>add<\/code> v\u00e0 <code>update<\/code> trung b\u00ecnh l\u00e0 O(1) cho m\u1ed7i ph\u1ea7n t\u1eed (do hash lookup\/insert). V\u1edbi <code>update<\/code>, t\u1ed5ng chi ph\u00ed ~ O(k) v\u1edbi k l\u00e0 s\u1ed1 ph\u1ea7n t\u1eed th\u00eam.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. X\u00f3a ph\u1ea7n t\u1eed v\u00e0 x\u1eed l\u00fd l\u1ed7i<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>remove(x)<\/code> \u2014 x\u00f3a ph\u1ea7n t\u1eed <code>x<\/code>, <strong>n\u1ebfu kh\u00f4ng t\u1ed3n t\u1ea1i s\u1ebd raise <code>KeyError<\/code><\/strong>.<\/li>\n\n\n\n<li><code>discard(x)<\/code> \u2014 x\u00f3a n\u1ebfu t\u1ed3n t\u1ea1i, <strong>kh\u00f4ng raise l\u1ed7i n\u1ebfu kh\u00f4ng t\u1ed3n t\u1ea1i<\/strong>.<\/li>\n\n\n\n<li><code>pop()<\/code> \u2014 x\u00f3a v\u00e0 tr\u1ea3 v\u1ec1 <em>m\u1ed9t<\/em> ph\u1ea7n t\u1eed <strong>ng\u1eabu nhi\u00ean<\/strong> (v\u00ec set kh\u00f4ng c\u00f3 th\u1ee9 t\u1ef1).<\/li>\n\n\n\n<li><code>clear()<\/code> \u2014 x\u00f3a to\u00e0n b\u1ed9 set.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>items = {1, 2, 3}\nitems.discard(4)   # safe even if 4 not present\ntry:\n    items.remove(4)  # will raise KeyError\nexcept KeyError:\n    print(\"4 is not in set\")\n    \nremoved = items.pop()   # removes an arbitrary element\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">items <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">items<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">discard<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">   <\/span><span style=\"color: #616E88\"># safe even if 4 not present<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">try<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    items<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">remove<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># will raise KeyError<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">except<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">KeyError<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">4 is not in set<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">removed <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> items<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">pop<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\">   <\/span><span style=\"color: #616E88\"># removes an arbitrary element<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Best practice:<\/strong> n\u1ebfu kh\u00f4ng ch\u1eafc ph\u1ea7n t\u1eed c\u00f3 t\u1ed3n t\u1ea1i hay kh\u00f4ng, d\u00f9ng <code>discard()<\/code> \u0111\u1ec3 tr\u00e1nh try\/except kh\u00f4ng c\u1ea7n thi\u1ebft.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Ph\u00e9p to\u00e1n t\u1eadp h\u1ee3p: method vs operator; in-place variants<\/h2>\n\n\n\n<p>Python h\u1ed7 tr\u1ee3 c\u1ea3 ph\u01b0\u01a1ng th\u1ee9c v\u00e0 to\u00e1n t\u1eed cho c\u00e1c ph\u00e9p to\u00e1n t\u1eadp h\u1ee3p. <strong>Quan tr\u1ecdng<\/strong> l\u00e0 ph\u00e2n bi\u1ec7t c\u00e1c thao t\u00e1c tr\u1ea3 v\u1ec1 set m\u1edbi vs thao t\u00e1c bi\u1ebfn \u0111\u1ed5i tr\u1ef1c ti\u1ebfp set hi\u1ec7n t\u1ea1i.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Tr\u1ea3 v\u1ec1 set m\u1edbi:\n<ul class=\"wp-block-list\">\n<li><code>A.union(B)<\/code> ho\u1eb7c <code>A | B<\/code><\/li>\n\n\n\n<li><code>A.intersection(B)<\/code> ho\u1eb7c <code>A &amp; B<\/code><\/li>\n\n\n\n<li><code>A.difference(B)<\/code> ho\u1eb7c <code>A - B<\/code><\/li>\n\n\n\n<li><code>A.symmetric_difference(B)<\/code> ho\u1eb7c <code>A ^ B<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Bi\u1ebfn \u0111\u1ed5i (in-place) set hi\u1ec7n t\u1ea1i:\n<ul class=\"wp-block-list\">\n<li><code>A.update(B)<\/code> t\u01b0\u01a1ng \u0111\u01b0\u01a1ng <code>A |= B<\/code><\/li>\n\n\n\n<li><code>A.intersection_update(B)<\/code> t\u01b0\u01a1ng \u0111\u01b0\u01a1ng <code>A &amp;= B<\/code><\/li>\n\n\n\n<li><code>A.difference_update(B)<\/code> t\u01b0\u01a1ng \u0111\u01b0\u01a1ng <code>A -= B<\/code><\/li>\n\n\n\n<li><code>A.symmetric_difference_update(B)<\/code> t\u01b0\u01a1ng \u0111\u01b0\u01a1ng <code>A ^= B<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>A = {1,2,3}\nB = {3,4,5}\n\nprint(A | B)  # {1,2,3,4,5}\nprint(A &amp; B)  # {3}\nprint(A - B)  # {1,2}\nprint(A ^ B)  # {1,2,4,5}\n\n# In-place example\nA |= {6,7}    # A is now {1,2,3,6,7}\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">A <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">B <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">A <\/span><span style=\"color: #81A1C1\">|<\/span><span style=\"color: #D8DEE9FF\"> B<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># {1,2,3,4,5}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">A <\/span><span style=\"color: #81A1C1\">&amp;<\/span><span style=\"color: #D8DEE9FF\"> B<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># {3}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">A <\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9FF\"> B<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># {1,2}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">A <\/span><span style=\"color: #81A1C1\">^<\/span><span style=\"color: #D8DEE9FF\"> B<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># {1,2,4,5}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># In-place example<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">A <\/span><span style=\"color: #81A1C1\">|=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">6<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">7<\/span><span style=\"color: #ECEFF4\">}<\/span><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #616E88\"># A is now {1,2,3,6,7}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Gi\u1ea3i th\u00edch:<\/strong> d\u00f9ng ph\u01b0\u01a1ng th\u1ee9c tr\u1ea3 v\u1ec1 m\u1edbi khi c\u1ea7n gi\u1eef nguy\u00ean b\u1ed9 ban \u0111\u1ea7u; d\u00f9ng in-place khi mu\u1ed1n c\u1eadp nh\u1eadt v\u00e0 tr\u00e1nh t\u1ea1o b\u1ea3n sao (ti\u1ebft ki\u1ec7m b\u1ed9 nh\u1edb).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Ki\u1ec3m tra quan h\u1ec7 t\u1eadp h\u1ee3p<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>issubset<\/code> \/ <code>&lt;=<\/code> : A l\u00e0 t\u1eadp con c\u1ee7a B<\/li>\n\n\n\n<li><code>issuperset<\/code> \/ <code>&gt;=<\/code> : A l\u00e0 t\u1eadp cha c\u1ee7a B<\/li>\n\n\n\n<li><code>isdisjoint<\/code> : kh\u00f4ng c\u00f3 ph\u1ea7n t\u1eed chung<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>X = {1,2}\nY = {1,2,3}\nprint(X.issubset(Y))  # True\nprint(Y.issuperset(X))# True\nprint(X.isdisjoint({4,5}))  # True<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">X <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">Y <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">X<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">issubset<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">Y<\/span><span style=\"color: #ECEFF4\">))<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">Y<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">issuperset<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">X<\/span><span style=\"color: #ECEFF4\">))<\/span><span style=\"color: #616E88\"># True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">X<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">isdisjoint<\/span><span style=\"color: #ECEFF4\">({<\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">}))<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># True<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">7. Duy\u1ec7t (iterate) v\u00e0 unordered property<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set l\u00e0 iterable, c\u00f3 th\u1ec3 d\u00f9ng <code>for<\/code> \u0111\u1ec3 duy\u1ec7t.<\/li>\n\n\n\n<li><strong>Kh\u00f4ng d\u00f9ng index<\/strong> (kh\u00f4ng support <code>s[0]<\/code>) v\u00ec set kh\u00f4ng c\u00f3 th\u1ee9 t\u1ef1.<\/li>\n\n\n\n<li>L\u01b0u \u00fd: th\u1ee9 t\u1ef1 duy\u1ec7t c\u00f3 th\u1ec3 kh\u00e1c nhau gi\u1eefa c\u00e1c l\u1ea7n ch\u1ea1y ho\u1eb7c phi\u00ean Python kh\u00e1c nhau.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>colors = {\"red\", \"green\", \"blue\"}\nfor color in colors:\n    print(color)  # order is arbitrary\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">colors <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">red<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">green<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">blue<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> color <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> colors<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">color<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># order is arbitrary<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Pitfall:<\/strong> kh\u00f4ng d\u1ef1a v\u00e0o th\u1ee9 t\u1ef1 khi thi\u1ebft k\u1ebf logic; n\u1ebfu c\u1ea7n th\u1ee9 t\u1ef1, d\u00f9ng <code>list<\/code> ho\u1eb7c <code>sorted()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. Chuy\u1ec3n \u0111\u1ed5i gi\u1eefa set v\u00e0 c\u00e1c ki\u1ec3u kh\u00e1c; sao ch\u00e9p<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>set()<\/code> \u2194 <code>list()<\/code> \/ <code>tuple()<\/code> \u0111\u1ec3 chuy\u1ec3n \u0111\u1ed5i.<\/li>\n\n\n\n<li><code>copy()<\/code> t\u1ea1o m\u1ed9t shallow copy. N\u1ebfu set ch\u1ee9a tuple (immutable), shallow copy \u0111\u1ee7; n\u1ebfu ch\u1ee9a tham chi\u1ebfu \u0111\u1ebfn c\u1ea5u tr\u00fac mutable trong tuple th\u00ec c\u1ea7n ch\u00fa \u00fd.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>lst = &#91;1,2,2,3&#93;\nuniq = set(lst)           # deduplicate\nback_to_list = list(uniq) # convert back to list\n\ns = {1, (2,3)}\ns_copy = s.copy()\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">lst <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">uniq <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">set<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">lst<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">           <\/span><span style=\"color: #616E88\"># deduplicate<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">back_to_list <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">list<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">uniq<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\"># convert back to list<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">s <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">)}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">s_copy <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> s<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">copy<\/span><span style=\"color: #ECEFF4\">()<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Ghi nh\u1edb:<\/strong> <code>s.copy()<\/code> l\u00e0 shallow copy; c\u00e1c ph\u1ea7n t\u1eed immutable th\u00ec an to\u00e0n.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9. <code>frozenset<\/code> \u2014 set b\u1ea5t bi\u1ebfn<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>frozenset<\/code> l\u00e0 phi\u00ean b\u1ea3n immutable c\u1ee7a set. C\u00f3 th\u1ec3 l\u00e0m key cho dict ho\u1eb7c ph\u1ea7n t\u1eed c\u1ee7a set kh\u00e1c (v\u00ec n\u00f3 hashable).<\/li>\n\n\n\n<li>T\u1ea1o: <code>fs = frozenset([1,2,3])<\/code><\/li>\n\n\n\n<li>Kh\u00f4ng c\u00f3 <code>add<\/code>\/<code>remove<\/code> v\u00ec b\u1ea5t bi\u1ebfn.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>fs = frozenset(&#91;1,2,3&#93;)\nmy_dict = {fs: \"value\"}  # allowed\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">fs <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">frozenset<\/span><span style=\"color: #ECEFF4\">(&#91;<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">&#93;)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">my_dict <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #D8DEE9FF\">fs<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">value<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">}<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># allowed<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Khi d\u00f9ng:<\/strong> d\u00f9ng frozenset khi c\u1ea7n kh\u00f3a c\u1ed1 \u0111\u1ecbnh ho\u1eb7c constant set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">10. Best practices &amp; pitfalls<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>D\u00f9ng <code>discard<\/code> thay v\u00ec <code>remove<\/code> n\u1ebfu kh\u00f4ng ch\u1eafc ph\u1ea7n t\u1eed t\u1ed3n t\u1ea1i.<\/li>\n\n\n\n<li>Kh\u00f4ng d\u1ef1a v\u00e0o th\u1ee9 t\u1ef1 c\u1ee7a set; n\u1ebfu c\u1ea7n order, d\u00f9ng <code>sorted()<\/code> ho\u1eb7c <code>list<\/code> + k\u1ef9 thu\u1eadt seen.<\/li>\n\n\n\n<li>S\u1eed d\u1ee5ng <code>frozenset<\/code> khi c\u1ea7n set l\u00e0m key dictionary ho\u1eb7c l\u00e0m ph\u1ea7n t\u1eed c\u1ee7a set kh\u00e1c.<\/li>\n\n\n\n<li>Khi l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u l\u1edbn, c\u00e2n nh\u1eafc b\u1ed9 nh\u1edb: set c\u00f3 overhead (hash table). Tuy nhi\u00ean n\u1ebfu b\u1ea1n c\u1ea7n membership test nhanh hay lo\u1ea1i b\u1ecf tr\u00f9ng, set th\u01b0\u1eddng \u0111\u00e1ng ti\u1ec1n.<\/li>\n\n\n\n<li>Tr\u00e1nh \u0111\u01b0a c\u00e1c ph\u1ea7n t\u1eed unhashable (list, dict) v\u00e0o set \u2014 s\u1ebd g\u00e2y <code>TypeError<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">11. K\u1ebft lu\u1eadn (phi\u00ean b\u1ea3n chuy\u00ean nghi\u1ec7p, s\u00fac t\u00edch)<\/h2>\n\n\n\n<p>Set l\u00e0 c\u00f4ng c\u1ee5 c\u1ef1c k\u1ef3 hi\u1ec7u qu\u1ea3 khi ta c\u1ea7n lo\u1ea1i b\u1ecf ph\u1ea7n t\u1eed tr\u00f9ng l\u1eb7p, ki\u1ec3m tra thu\u1ed9c t\u00ednh nhanh hay th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p to\u00e1n t\u1eadp h\u1ee3p. V\u00ec underlying l\u00e0 hash table, set cung c\u1ea5p <em>membership test<\/em> v\u00e0 thao t\u00e1c th\u00eam\/x\u00f3a v\u1edbi chi ph\u00ed trung b\u00ecnh r\u1ea5t th\u1ea5p (O(1)), n\u00ean th\u01b0\u1eddng l\u00e0 l\u1ef1a ch\u1ecdn h\u00e0ng \u0111\u1ea7u cho c\u00e1c b\u00e0i to\u00e1n c\u1ea7n truy v\u1ea5n nhi\u1ec1u.<\/p>\n\n\n\n<p>Tuy nhi\u00ean set c\u0169ng c\u00f3 h\u1ea1n ch\u1ebf: <strong>kh\u00f4ng c\u00f3 th\u1ee9 t\u1ef1<\/strong> v\u00e0 ch\u1ec9 ch\u1ee9a ph\u1ea7n t\u1eed <strong>hashable<\/strong>. V\u00ec v\u1eady trong thi\u1ebft k\u1ebf h\u1ec7 th\u1ed1ng ta c\u1ea7n c\u00e2n nh\u1eafc: n\u1ebfu c\u1ea7n th\u1ee9 t\u1ef1 ho\u1eb7c ph\u00e9p truy c\u1eadp theo index, n\u00ean d\u00f9ng list\/tuple; n\u1ebfu c\u1ea7n key c\u00f3 th\u1ec3 d\u00f9ng dict; n\u1ebfu c\u1ea7n key immutable (v\u00ed d\u1ee5 l\u00e0m h\u1eb1ng s\u1ed1 c\u1ea5u h\u00ecnh), d\u00f9ng <code>frozenset<\/code>.<\/p>\n\n\n\n<p>N\u1eafm v\u1eefng c\u00e1c ph\u01b0\u01a1ng th\u1ee9c (v\u00e0 in-place variants) c\u00f9ng hi\u1ec3u r\u00f5 \u0111\u1ed9 ph\u1ee9c t\u1ea1p, c\u00e1c l\u1ed7i ph\u1ed5 bi\u1ebfn (v\u00ed d\u1ee5 <code>remove<\/code> vs <code>discard<\/code>), v\u00e0 bi\u1ebft c\u00e1ch k\u1ebft h\u1ee3p set v\u1edbi c\u00e1c k\u1ef9 thu\u1eadt kh\u00e1c (set comprehension, tr\u00ecnh t\u1ef1 gi\u1eef th\u1ee9 t\u1ef1 khi dedupe) s\u1ebd gi\u00fap b\u1ea1n vi\u1ebft code Python ch\u00ednh x\u00e1c, hi\u1ec7u qu\u1ea3 v\u00e0 chuy\u00ean nghi\u1ec7p h\u01a1n.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">12. T\u00e0i li\u1ec7u tham kh\u1ea3o<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Python Software Foundation.<\/strong> <em>The Python Standard Library: Built-in Types \u2013 Set.<\/em> Python Official Documentation. Truy c\u1eadp t\u1ea1i: <a>https:\/\/docs.python.org\/3\/library\/stdtypes.html#set<\/a><\/li>\n\n\n\n<li><strong>Real Python.<\/strong> <em>Sets in Python: How to Use Them Effectively.<\/em> Real Python Tutorial. Truy c\u1eadp t\u1ea1i: <a>https:\/\/realpython.com\/python-sets\/<\/a><\/li>\n\n\n\n<li><strong>W3Schools.<\/strong> <em>Python Sets.<\/em> W3Schools Online Tutorials. Truy c\u1eadp t\u1ea1i: <a>https:\/\/www.w3schools.com\/python\/python_sets.asp<\/a><\/li>\n\n\n\n<li><strong>GeeksforGeeks.<\/strong> <em>Python Set \u2013 Methods and Operations.<\/em> GeeksforGeeks Programming Resource. Truy c\u1eadp t\u1ea1i: <a>https:\/\/www.geeksforgeeks.org\/python-set-methods\/<\/a><\/li>\n\n\n\n<li><strong>Luciano Ramalho.<\/strong> <em>Fluent Python (2nd Edition).<\/em> O\u2019Reilly Media, 2022. \u2014 Ch\u01b0\u01a1ng 3: D\u1eef li\u1ec7u t\u1eadp h\u1ee3p v\u00e0 \u00e1nh x\u1ea1 (Sets and Mappings).<\/li>\n\n\n\n<li>Python for Professionals: Learning Python as a Second Language: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710002222624882405978&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-for-professionals-3\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-for-professionals-3<\/a><\/li>\n\n\n\n<li>Python: Deeper Insights into Machine Learning: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710015810095319857183&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-deeper-insights-into-machine-learning\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-deeper-insights-into-machine-learning<\/a><\/li>\n\n\n\n<li>DataFusion Python Bindings in Practice: The Complete Guide for Developers and Engineers: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710049093362364820452&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fdatafusion-python-bindings-in-practice\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/datafusion-python-bindings-in-practice<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Set l\u00e0 m\u1ed9t trong nh\u1eefng c\u1ea5u tr\u00fac d\u1eef li\u1ec7u r\u1ea5t h\u1eefu \u00edch trong Python: n\u00f3 l\u01b0u&hellip;<\/p>\n","protected":false},"author":1,"featured_media":824,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowieHDDA:productID":"","footnotes":""},"categories":[41],"tags":[40],"class_list":["post-1621","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kien-thuc-lap-trinh","tag-python-co-ban"],"_links":{"self":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/comments?post=1621"}],"version-history":[{"count":2,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1621\/revisions"}],"predecessor-version":[{"id":2464,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1621\/revisions\/2464"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media\/824"}],"wp:attachment":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media?parent=1621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/categories?post=1621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/tags?post=1621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}