{"id":3464,"date":"2026-11-26T06:09:10","date_gmt":"2026-11-26T06:09:10","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3464"},"modified":"2026-11-26T06:09:10","modified_gmt":"2026-11-26T06:09:10","slug":"python-dictionary-fromkeys","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-dictionary-fromkeys\/","title":{"rendered":"Python Dictionary fromkeys() Method with Examples"},"content":{"rendered":"<p>The <code>dict.fromkeys()<\/code> method creates a <strong>new dictionary<\/strong> from a sequence of keys, giving every key the same value. It is handy for setting up counters, flags or default settings in one line. This page explains the syntax with outputs, shows the one trap every beginner falls into (mutable values), and covers two useful tricks: removing duplicates and building a dictionary from two lists.<\/p>\n<h2>Syntax<\/h2>\n<pre class=\"wp-block-code\"><code>dict.fromkeys(iterable, value=None)<\/code><\/pre>\n<ul>\n<li><code>iterable<\/code>: the keys (a list, tuple, string, set and so on).<\/li>\n<li><code>value<\/code>: the value given to every key. If you leave it out, it is <code>None<\/code>.<\/li>\n<li>It returns a new dictionary; nothing existing is changed.<\/li>\n<\/ul>\n<h2>Basic Examples<\/h2>\n<pre class=\"wp-block-code\"><code>keys = ['a', 'b', 'c']\n\nprint(dict.fromkeys(keys, 0))\nprint(dict.fromkeys(keys))\nprint(dict.fromkeys('xyz', 1))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{'a': 0, 'b': 0, 'c': 0}\n{'a': None, 'b': None, 'c': None}\n{'x': 1, 'y': 1, 'z': 1}<\/code><\/pre>\n<p>In the last line, a string is iterable, so each character becomes a key.<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-fromkeys-METHOD.jpg\" alt=\"Python dictionary fromkeys method example\" \/><\/figure>\n<h2>A Practical Use: Setting Up Attendance<\/h2>\n<pre class=\"wp-block-code\"><code>students = ['Riya', 'Aman', 'Kabir']\nattendance = dict.fromkeys(students, 'Absent')\nattendance['Aman'] = 'Present'\nprint(attendance)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{'Riya': 'Absent', 'Aman': 'Present', 'Kabir': 'Absent'}<\/code><\/pre>\n<h2>The Trap: Using a List (or Any Mutable Value)<\/h2>\n<p><code>fromkeys()<\/code> does not make a copy of the value for each key. Every key points to the <strong>same<\/strong> object. With numbers and strings this does not matter, but with a list it causes a surprising bug:<\/p>\n<pre class=\"wp-block-code\"><code>d = dict.fromkeys(['a', 'b', 'c'], [])\nd['a'].append(1)\nprint(d)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{'a': [1], 'b': [1], 'c': [1]}<\/code><\/pre>\n<p>Adding to <code>d['a']<\/code> changed all three, because they share one list. Use a <strong>dictionary comprehension<\/strong> instead, which creates a fresh list for each key:<\/p>\n<pre class=\"wp-block-code\"><code>d = {k: [] for k in ['a', 'b', 'c']}\nd['a'].append(1)\nprint(d)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{'a': [1], 'b': [], 'c': []}<\/code><\/pre>\n<h2>Keys and Values from Two Lists? Use zip(), Not fromkeys()<\/h2>\n<p>If you pass a list as the value, every key gets the whole list:<\/p>\n<pre class=\"wp-block-code\"><code>keys = ['a', 'b', 'c']\nvalues = [10, 20, 30]\n\nprint(dict.fromkeys(keys, values))\nprint(dict(zip(keys, values)))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{'a': [10, 20, 30], 'b': [10, 20, 30], 'c': [10, 20, 30]}\n{'a': 10, 'b': 20, 'c': 30}<\/code><\/pre>\n<p><code>zip()<\/code> pairs the items one by one, and <code>dict()<\/code> turns those pairs into a dictionary.<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-fromkeys-METHOD-2.jpg\" alt=\"Python dict from two lists using zip\" \/><\/figure>\n<h2>Trick: Remove Duplicates and Keep the Order<\/h2>\n<p>Dictionary keys are unique and, since Python 3.7, keep their insertion order. So <code>fromkeys()<\/code> is a neat way to remove duplicates from a list without changing the order:<\/p>\n<pre class=\"wp-block-code\"><code>items = ['apple', 'banana', 'apple', 'cherry', 'banana']\nprint(list(dict.fromkeys(items)))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>['apple', 'banana', 'cherry']<\/code><\/pre>\n<p>Converting to a <code>set<\/code> also removes duplicates, but a set does not keep the original order.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>What is the default value in fromkeys()?<\/h3>\n<p><code>None<\/code>.<\/p>\n<h3>Does fromkeys() change an existing dictionary?<\/h3>\n<p>No. It is a class method that always returns a new dictionary.<\/p>\n<h3>Why did all my keys change when I updated one list?<\/h3>\n<p>Because <code>fromkeys()<\/code> gives every key the same list object. Use <code>{k: [] for k in keys}<\/code> instead.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/python-dictionary-setdefault\/\">Python dictionary setdefault()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-list-sort\/\">Python list sort()<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The dict.fromkeys() method creates a new dictionary from a sequence of keys, giving every key the same value. It is handy for setting up counters, flags or default settings in one line. This page explains the syntax with outputs, shows the one trap every beginner falls into (mutable values), and covers two useful tricks: removing &#8230; <a title=\"Python Dictionary fromkeys() Method with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-dictionary-fromkeys\/\" aria-label=\"Read more about Python Dictionary fromkeys() Method with Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":["post-3464","post","type-post","status-publish","format-standard","hentry","category-python-tutorial","tag-python","tag-python-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3464"}],"version-history":[{"count":4,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3464\/revisions"}],"predecessor-version":[{"id":6160,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3464\/revisions\/6160"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}