{"id":356,"date":"2025-03-12T13:58:24","date_gmt":"2025-03-12T13:58:24","guid":{"rendered":"https:\/\/news.techjunior.vn\/?p=356"},"modified":"2025-03-12T13:58:24","modified_gmt":"2025-03-12T13:58:24","slug":"%f0%9f%93%a2-list-comprehension-bi-kip-viet-code-python-gon-hon-chay-nhanh-hon-%f0%9f%9a%80%f0%9f%90%8d","status":"publish","type":"post","link":"https:\/\/news.techjunior.vn\/?p=356","title":{"rendered":"\ud83d\udce2 LIST COMPREHENSION \u2013 B\u00cd K\u00cdP VI\u1ebeT CODE PYTHON G\u1eccN H\u01a0N, CH\u1ea0Y NHANH H\u01a0N! \ud83d\ude80\ud83d\udc0d"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"817\" height=\"500\" src=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/list.jpg\" alt=\"\" class=\"wp-image-357\" srcset=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/list.jpg 817w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/list-300x184.jpg 300w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/list-768x470.jpg 768w\" sizes=\"auto, (max-width: 817px) 100vw, 817px\" \/><\/figure>\n\n\n\n<p>N\u1ebfu b\u1ea1n \u0111\u00e3 t\u1eebng vi\u1ebft v\u00f2ng l\u1eb7p trong Python \u0111\u1ec3 t\u1ea1o danh s\u00e1ch, ch\u1eafc h\u1eb3n b\u1ea1n s\u1ebd th\u00edch th\u00fa v\u1edbi <strong>List Comprehension<\/strong> \u2013 m\u1ed9t c\u00e1ch vi\u1ebft ng\u1eafn g\u1ecdn v\u00e0 t\u1ed1i \u01b0u h\u01a1n r\u1ea5t nhi\u1ec1u! H\u00f4m nay, h\u00e3y c\u00f9ng kh\u00e1m ph\u00e1 c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd n\u00e0y nh\u00e9! \ud83d\udd25<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0e <strong>1. List Comprehension l\u00e0 g\u00ec?<\/strong><\/h3>\n\n\n\n<p><strong>List Comprehension<\/strong> l\u00e0 m\u1ed9t c\u00e1ch vi\u1ebft r\u00fat g\u1ecdn gi\u00fap b\u1ea1n t\u1ea1o danh s\u00e1ch m\u1edbi t\u1eeb danh s\u00e1ch c\u00f3 s\u1eb5n ho\u1eb7c t\u1eeb m\u1ed9t v\u00f2ng l\u1eb7p, v\u1edbi c\u00fa ph\u00e1p \u0111\u01a1n gi\u1ea3n v\u00e0 d\u1ec5 \u0111\u1ecdc h\u01a1n so v\u1edbi c\u00e1ch th\u00f4ng th\u01b0\u1eddng.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udd39 <strong>C\u00fa ph\u00e1p c\u01a1 b\u1ea3n:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;bi\u1ec3u_th\u1ee9c for bi\u1ebfn in danh_s\u00e1ch if \u0111i\u1ec1u_ki\u1ec7n]\n<\/code><\/pre>\n\n\n\n<p>Trong \u0111\u00f3:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>bi\u1ec3u_th\u1ee9c<\/code> l\u00e0 gi\u00e1 tr\u1ecb \u0111\u01b0\u1ee3c th\u00eam v\u00e0o danh s\u00e1ch m\u1edbi.<\/li>\n\n\n\n<li><code>for bi\u1ebfn in danh_s\u00e1ch<\/code> l\u00e0 v\u00f2ng l\u1eb7p \u0111\u1ec3 duy\u1ec7t qua danh s\u00e1ch g\u1ed1c.<\/li>\n\n\n\n<li><code>if \u0111i\u1ec1u_ki\u1ec7n<\/code> (t\u00f9y ch\u1ecdn) gi\u00fap l\u1ecdc c\u00e1c ph\u1ea7n t\u1eed theo \u0111i\u1ec1u ki\u1ec7n nh\u1ea5t \u0111\u1ecbnh.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udee0 <strong>2. V\u00ec sao n\u00ean d\u00f9ng List Comprehension?<\/strong><\/h3>\n\n\n\n<p>\u2705 <strong>Vi\u1ebft code ng\u1eafn g\u1ecdn h\u01a1n<\/strong> \u2013 Gi\u00fap ti\u1ebft ki\u1ec7m th\u1eddi gian v\u00e0 d\u1ec5 \u0111\u1ecdc h\u01a1n.<br>\u2705 <strong>Ch\u1ea1y nhanh h\u01a1n<\/strong> \u2013 Hi\u1ec7u su\u1ea5t t\u1ed1t h\u01a1n so v\u1edbi v\u00f2ng l\u1eb7p th\u00f4ng th\u01b0\u1eddng.<br>\u2705 <strong>T\u1ea1o danh s\u00e1ch linh ho\u1ea1t<\/strong> \u2013 D\u1ec5 d\u00e0ng th\u00eam \u0111i\u1ec1u ki\u1ec7n l\u1ecdc ho\u1eb7c x\u1eed l\u00fd d\u1eef li\u1ec7u.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd25 <strong>3. \u1ee8ng d\u1ee5ng c\u1ee7a List Comprehension<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u2728 <strong>3.1. T\u1ea1o danh s\u00e1ch m\u1edbi t\u1eeb danh s\u00e1ch c\u00f3 s\u1eb5n<\/strong><\/h4>\n\n\n\n<p>Thay v\u00ec d\u00f9ng v\u00f2ng l\u1eb7p \u0111\u1ec3 t\u1ea1o danh s\u00e1ch m\u1edbi, b\u1ea1n c\u00f3 th\u1ec3 l\u00e0m \u0111i\u1ec1u \u0111\u00f3 ch\u1ec9 v\u1edbi m\u1ed9t d\u00f2ng code!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># T\u1ea1o danh s\u00e1ch c\u00e1c s\u1ed1 t\u1eeb 1 \u0111\u1ebfn 10\nnumbers = &#091;x for x in range(1, 11)]\nprint(numbers)  \n# K\u1ebft qu\u1ea3: &#091;1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udd0d <strong>3.2. \u00c1p d\u1ee5ng \u0111i\u1ec1u ki\u1ec7n l\u1ecdc danh s\u00e1ch<\/strong><\/h4>\n\n\n\n<p>B\u1ea1n c\u00f3 th\u1ec3 l\u1ecdc c\u00e1c ph\u1ea7n t\u1eed trong danh s\u00e1ch b\u1eb1ng c\u00e1ch th\u00eam \u0111i\u1ec1u ki\u1ec7n <code>if<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># L\u1ecdc c\u00e1c s\u1ed1 ch\u1eb5n t\u1eeb danh s\u00e1ch\neven_numbers = &#091;x for x in range(1, 11) if x % 2 == 0]\nprint(even_numbers)  \n# K\u1ebft qu\u1ea3: &#091;2, 4, 6, 8, 10]\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83c\udfaf <strong>3.3. Chuy\u1ec3n \u0111\u1ed5i d\u1eef li\u1ec7u trong danh s\u00e1ch<\/strong><\/h4>\n\n\n\n<p>B\u1ea1n c\u00f3 th\u1ec3 thay \u0111\u1ed5i gi\u00e1 tr\u1ecb c\u1ee7a c\u00e1c ph\u1ea7n t\u1eed khi th\u00eam v\u00e0o danh s\u00e1ch m\u1edbi.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># B\u00ecnh ph\u01b0\u01a1ng c\u00e1c s\u1ed1 t\u1eeb 1 \u0111\u1ebfn 10\nsquares = &#091;x**2 for x in range(1, 11)]\nprint(squares)  \n# K\u1ebft qu\u1ea3: &#091;1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udee0 <strong>3.4. L\u00e0m vi\u1ec7c v\u1edbi chu\u1ed7i k\u00fd t\u1ef1<\/strong><\/h4>\n\n\n\n<p>B\u1ea1n c\u0169ng c\u00f3 th\u1ec3 \u00e1p d\u1ee5ng List Comprehension \u0111\u1ec3 x\u1eed l\u00fd chu\u1ed7i.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># L\u1ea5y danh s\u00e1ch c\u00e1c ch\u1eef c\u00e1i vi\u1ebft hoa trong chu\u1ed7i\ntext = \"Hello Python\"\nuppercase_letters = &#091;char for char in text if char.isupper()]\nprint(uppercase_letters)  \n# K\u1ebft qu\u1ea3: &#091;'H', 'P']\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83c\udfc6 <strong>3.5. D\u00f9ng List Comprehension v\u1edbi nhi\u1ec1u v\u00f2ng l\u1eb7p (Nested Loops)<\/strong><\/h4>\n\n\n\n<p>B\u1ea1n c\u00f3 th\u1ec3 l\u1ed3ng nhi\u1ec1u v\u00f2ng l\u1eb7p \u0111\u1ec3 t\u1ea1o danh s\u00e1ch t\u1eeb nhi\u1ec1u danh s\u00e1ch con.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># T\u1ea1o danh s\u00e1ch ch\u1ee9a t\u1ea5t c\u1ea3 c\u1eb7p (x, y) t\u1eeb hai danh s\u00e1ch\npairs = &#091;(x, y) for x in range(1, 4) for y in range(5, 8)]\nprint(pairs)  \n# K\u1ebft qu\u1ea3: &#091;(1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (3, 5), (3, 6), (3, 7)]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>4. Khi n\u00e0o KH\u00d4NG n\u00ean d\u00f9ng List Comprehension?<\/strong><\/h3>\n\n\n\n<p>D\u00f9 List Comprehension r\u1ea5t ti\u1ec7n l\u1ee3i, nh\u01b0ng kh\u00f4ng ph\u1ea3i l\u00fac n\u00e0o c\u0169ng n\u00ean s\u1eed d\u1ee5ng. H\u00e3y <strong>tr\u00e1nh d\u00f9ng<\/strong> khi:<br>\u274c <strong>Bi\u1ec3u th\u1ee9c qu\u00e1 ph\u1ee9c t\u1ea1p<\/strong> \u2013 N\u1ebfu qu\u00e1 nhi\u1ec1u \u0111i\u1ec1u ki\u1ec7n ho\u1eb7c v\u00f2ng l\u1eb7p l\u1ed3ng nhau, code s\u1ebd kh\u00f3 \u0111\u1ecdc.<br>\u274c <strong>L\u00e0m thay \u0111\u1ed5i tr\u1ea1ng th\u00e1i b\u00ean ngo\u00e0i<\/strong> \u2013 N\u1ebfu b\u1ea1n c\u1ea7n c\u1eadp nh\u1eadt bi\u1ebfn b\u00ean ngo\u00e0i ho\u1eb7c thao t\u00e1c v\u1edbi file, h\u00e3y d\u00f9ng v\u00f2ng l\u1eb7p th\u00f4ng th\u01b0\u1eddng.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5, n\u1ebfu List Comprehension qu\u00e1 d\u00e0i v\u00e0 kh\u00f3 hi\u1ec3u, b\u1ea1n n\u00ean t\u00e1ch ra vi\u1ebft b\u1eb1ng v\u00f2ng l\u1eb7p \u0111\u1ec3 d\u1ec5 \u0111\u1ecdc h\u01a1n.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># C\u00e1ch kh\u00f4ng n\u00ean d\u00f9ng - qu\u00e1 ph\u1ee9c t\u1ea1p!\nresult = &#091;x**2 for x in range(1, 11) if x % 2 == 0 if x &gt; 4]\n<\/code><\/pre>\n\n\n\n<p>N\u00ean vi\u1ebft l\u1ea1i b\u1eb1ng v\u00f2ng l\u1eb7p \u0111\u1ec3 d\u1ec5 hi\u1ec3u h\u01a1n:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>result = &#091;]\nfor x in range(1, 11):\n    if x % 2 == 0 and x &gt; 4:\n        result.append(x**2)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 <strong>5. T\u1ed5ng k\u1ebft<\/strong><\/h3>\n\n\n\n<p>\ud83d\udd39 <strong>List Comprehension<\/strong> gi\u00fap vi\u1ebft code ng\u1eafn g\u1ecdn, d\u1ec5 \u0111\u1ecdc v\u00e0 hi\u1ec7u qu\u1ea3 h\u01a1n.<br>\ud83d\udd39 B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng n\u00f3 \u0111\u1ec3 t\u1ea1o danh s\u00e1ch, l\u1ecdc d\u1eef li\u1ec7u v\u00e0 chuy\u1ec3n \u0111\u1ed5i d\u1eef li\u1ec7u m\u1ed9t c\u00e1ch nhanh ch\u00f3ng.<br>\ud83d\udd39 Tuy nhi\u00ean, \u0111\u1eebng l\u1ea1m d\u1ee5ng khi logic qu\u00e1 ph\u1ee9c t\u1ea1p, h\u00e3y lu\u00f4n \u0111\u1ea3m b\u1ea3o code d\u1ec5 hi\u1ec3u.<\/p>\n\n\n\n<p>B\u1ea1n \u0111\u00e3 s\u1eed d\u1ee5ng List Comprehension ch\u01b0a? H\u00e3y th\u1eed ngay v\u00e0 chia s\u1ebb c\u1ea3m nh\u1eadn c\u1ee7a b\u1ea1n nh\u00e9! \u2b07\ufe0f\ud83d\udcac<\/p>\n\n\n\n<p>#Python #LapTrinhPython #ListComprehension #CodeGonGang #PythonTips<\/p>\n","protected":false},"excerpt":{"rendered":"<p>N\u1ebfu b\u1ea1n \u0111\u00e3 t\u1eebng vi\u1ebft v\u00f2ng l\u1eb7p trong Python \u0111\u1ec3 t\u1ea1o danh s\u00e1ch, ch\u1eafc h\u1eb3n b\u1ea1n s\u1ebd th\u00edch th\u00fa v\u1edbi List Comprehension \u2013 m\u1ed9t c\u00e1ch vi\u1ebft ng\u1eafn g\u1ecdn v\u00e0 t\u1ed1i \u01b0u h\u01a1n&#8230;<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-356","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=356"}],"version-history":[{"count":1,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/356\/revisions"}],"predecessor-version":[{"id":358,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/356\/revisions\/358"}],"wp:attachment":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}