{"id":75163,"date":"2020-01-14T10:50:04","date_gmt":"2020-01-14T05:20:04","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75163"},"modified":"2020-01-14T10:50:04","modified_gmt":"2020-01-14T05:20:04","slug":"python-collections","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-collections\/","title":{"rendered":"Python Collections &#8211; The unordered and unindexed data structures"},"content":{"rendered":"<p>Today we are going to learn about Python Collections. These include <strong>sets<\/strong> and <strong>dictionaries<\/strong>.<\/p>\n<p>So let\u2019s get started.<\/p>\n<h3>Python Collections<\/h3>\n<p>Unlike sequences, Python collections <strong>do not<\/strong> have any <strong>order<\/strong>. They are <strong>unordered<\/strong> and <strong>unindexed<\/strong> data structures. This means that it is <strong>not necessary<\/strong> to get the order in which we <strong>insert items<\/strong>.<\/p>\n<h3>Types of Python Collections<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-collections.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-75498 aligncenter\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-collections.jpg\" alt=\"python collections\" width=\"802\" height=\"420\" \/><\/a><\/h3>\n<p>Python collections consist of <strong>Set<\/strong> and <strong>Dictionary<\/strong>.<\/p>\n<h4>Python Sets<\/h4>\n<p>The sets data structure in Python is inspired by the <strong>mathematical sets concept<\/strong>. A set is a collection of <strong>distinct<\/strong> <strong>elements<\/strong>, so we don\u2019t have any <strong>duplicates<\/strong> in a set.<\/p>\n<p>We can declare a set by separating values with <strong>commas<\/strong> inside<strong> curly braces {}<\/strong> or by using<strong> the set() function<\/strong>. To create an <strong>empty set<\/strong>, you must use <strong>set()<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">setA = {1,2,3,4,5,4}\nsetB = set({10,20,30,20,30})\nsetC = set()\n\nprint(setA)\nprint(setB)\nprint(setC)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1,2,3,4,5}<br \/>\n{10,20,30}<br \/>\nset()<\/div>\n<p>Sets is a <strong>mutable<\/strong> data structure so you can <strong>add<\/strong>, <strong>update<\/strong>, or <strong>remove elements<\/strong> from the set. But a set cannot contain <strong>lists<\/strong>, <strong>dictionary<\/strong> or <strong>sets<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">setA = {1,4,5,2}\nsetA.remove(5)\nsetA.add(3)\nprint( setA)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1,2,3,4}<\/div>\n<h4>Python Dictionary<\/h4>\n<p>Python dictionaries are an <strong>unordered<\/strong> collection of objects that holds <strong>value<\/strong> in a key-value structure. The key should be <strong>unique<\/strong> and must be an <strong>immutable<\/strong> object like a <strong>number<\/strong>, <strong>strings<\/strong>, and <strong>tuples<\/strong>. The values can be any Python object.<\/p>\n<p>We can declare the Python dictionary in <strong>two ways<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">dict1 = { 1: \u201dOne\u201d, 2: \u201dTwo\u201d, 3: \u201dThree\u201d}\n\ndict2 = dict()\ndict2[1] = \u201cOne\u201d\ndict2[2] = \u201cTwo\u201d\ndict2[3] = \u201cThree\u201d\n\nprint(dict1)\nprint(dict2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{1: &#8216;One&#8217;, 2: &#8216;Two&#8217;, 3: &#8216;Three&#8217;}<br \/>\n{1: &#8216;One&#8217;, 2: &#8216;Two&#8217;, 3: &#8216;Three&#8217;}<\/div>\n<p>The main purpose of a dictionary is to map <strong>unique keys<\/strong> with a value so that <strong>retrieving information<\/strong> is optimized when we know the key.<\/p>\n<p>Dictionaries are <strong>mutable<\/strong> and we can simply add new <strong>key-value pairs<\/strong> in the dictionary by <strong>assigning<\/strong> them.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">d = {\u201cShrangi\u201d: [1, \u201cPCM\u201d], \u201cAkshay\u201d: [2,\u201dPCB\u201d], \u201cHimanshu\u201d: [3, \u201cArts\u201d]}\nd[\u201cAyushi\u201d] = [4,\u201dCommerce\u201d]\nprint(d)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">{&#8216;Shrangi&#8217;: [1, &#8216;PCM&#8217;], &#8216;Akshay&#8217;: [2, &#8216;PCB&#8217;], &#8216;Himanshu&#8217;: [3, &#8216;Arts&#8217;]}<br \/>\n{&#8216;Shrangi&#8217;: [1, &#8216;PCM&#8217;], &#8216;Akshay&#8217;: [2, &#8216;PCB&#8217;], &#8216;Himanshu&#8217;: [3, &#8216;Arts&#8217;], &#8216;Ayushi&#8217;: [4, &#8216;Commerce&#8217;]}<\/div>\n<h3>Summary<\/h3>\n<p>In today\u2019s article, we have learned about <strong>Collections in Python<\/strong>. We saw the different types of collections, that is, <strong>sets<\/strong> and <strong>dictionaries<\/strong>. They do not have any <strong>specific ordering<\/strong> of <strong>elements<\/strong>.<\/p>\n<p>We understood the purpose of each of these <strong>data structures<\/strong> and then saw examples of them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we are going to learn about Python Collections. These include sets and dictionaries. So let\u2019s get started. Python Collections Unlike sequences, Python collections do not have any order. They are unordered and unindexed&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75498,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1301,1196,1302,1164,1303,1282,1283,1304],"class_list":["post-75163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-collections-in-python","tag-dictionaries-in-python","tag-python-collections","tag-python-dictionaries","tag-python-dictionary","tag-python-sets","tag-sets-in-python","tag-types-of-python-collections"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Collections - The unordered and unindexed data structures - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python collections is the unordered and unindexed data structures in Python which includes two types, that is, Python sets &amp; dictionaries. Learn about it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-collections\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Collections - The unordered and unindexed data structures - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python collections is the unordered and unindexed data structures in Python which includes two types, that is, Python sets &amp; dictionaries. Learn about it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-collections\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-14T05:20:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-collections.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Collections - The unordered and unindexed data structures - TechVidvan","description":"Python collections is the unordered and unindexed data structures in Python which includes two types, that is, Python sets & dictionaries. Learn about it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-collections\/","og_locale":"en_US","og_type":"article","og_title":"Python Collections - The unordered and unindexed data structures - TechVidvan","og_description":"Python collections is the unordered and unindexed data structures in Python which includes two types, that is, Python sets & dictionaries. Learn about it.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-collections\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-14T05:20:04+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-collections.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-collections\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-collections\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Collections &#8211; The unordered and unindexed data structures","datePublished":"2020-01-14T05:20:04+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-collections\/"},"wordCount":330,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-collections\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-collections.jpg","keywords":["collections in python","dictionaries in python","python collections","python dictionaries","python dictionary","Python Sets","sets in python","Types of Python Collections"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-collections\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-collections\/","url":"https:\/\/techvidvan.com\/tutorials\/python-collections\/","name":"Python Collections - The unordered and unindexed data structures - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-collections\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-collections\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-collections.jpg","datePublished":"2020-01-14T05:20:04+00:00","description":"Python collections is the unordered and unindexed data structures in Python which includes two types, that is, Python sets & dictionaries. Learn about it.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-collections\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-collections\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-collections\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-collections.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-collections.jpg","width":802,"height":420,"caption":"python collections"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-collections\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Collections &#8211; The unordered and unindexed data structures"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=75163"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75163\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75498"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}