{"id":18,"date":"2023-09-04T13:54:55","date_gmt":"2023-09-04T12:54:55","guid":{"rendered":"https:\/\/onlinecodinghelp.com\/?p=18"},"modified":"2023-09-11T13:27:37","modified_gmt":"2023-09-11T12:27:37","slug":"difference-between-list-tuple-and-dictionary-in-python","status":"publish","type":"post","link":"https:\/\/onlinecodinghelp.com\/index.php\/2023\/09\/04\/difference-between-list-tuple-and-dictionary-in-python\/","title":{"rendered":"What is difference between List Tuple and dictionary In python"},"content":{"rendered":"\n<p>In Python, lists, tuples, and dictionaries are three different data structures, each with its own characteristics and use cases. Here&#8217;s a brief overview of the differences between them<\/p>\n\n\n\n<p class=\"has-large-font-size\"><strong><code>List<\/code><\/strong><\/p>\n\n\n\n<p>Lists are ordered collections of items.<\/p>\n\n\n\n<p>They are mutable, which means you can change their contents (add, remove, or modify elements) after they are created.<\/p>\n\n\n\n<p>Lists are defined using square brackets &#8221; [ ] &#8220;<\/p>\n\n\n\n<p>Example: <code>test_list = [1, 2, 3, 4<\/code>]<\/p>\n\n\n\n<p class=\"has-large-font-size\"><strong><code>Tuple<\/code><\/strong><\/p>\n\n\n\n<p>Tuples are ordered collections of items, just like lists.<\/p>\n\n\n\n<p>However, they are immutable, which means once you create a tuple, you cannot change its contents<\/p>\n\n\n\n<p>Tuples are defined using parentheses <code>()<\/code><\/p>\n\n\n\n<p>Example: <code>my_tuple = (1, 2, 3, 4)<\/code><\/p>\n\n\n\n<p class=\"has-large-font-size\"><strong>Dictionaries<\/strong><\/p>\n\n\n\n<p>Dictionaries are unordered collections of key-value pairs. Instead of using indices like lists and tuples, you access elements in a dictionary using keys.<\/p>\n\n\n\n<p>Dictionaries are mutable, so you can add, modify, or remove key-value pairs<\/p>\n\n\n\n<p>Dictionaries are defined using curly braces <code>{}<\/code> or with the <code>dict()<\/code> constructor.<\/p>\n\n\n\n<p>Example: <code>my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}<\/code><\/p>\n\n\n\n<p><strong>Here&#8217;s a comparison table to summarize the differences<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"653\" height=\"167\" src=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/image.png\" alt=\"List Tuple Dictionary In Python\" class=\"wp-image-19\" srcset=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/image.png 653w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/image-300x77.png 300w\" sizes=\"(max-width: 653px) 100vw, 653px\" \/><\/figure>\n\n\n\n<p>Choose the appropriate data structure based on your specific needs. Use lists when you need an ordered collection of items that can change. Use tuples when you need an ordered collection that should remain constant. Use dictionaries when you need to store key-value pairs and want to quickly access values based on their keys<\/p>\n\n\n\n<p>See What is list on click below<br><a href=\"https:\/\/onlinecodinghelp.com\/index.php\/2023\/09\/06\/what-is-list-in-python\/\">https:\/\/onlinecodinghelp.com\/index.php\/2023\/09\/06\/what-is-list-in-python\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python, lists, tuples, and dictionaries are three different data structures, each with its own&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts\/18"}],"collection":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":3,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":123,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions\/123"}],"wp:attachment":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}