<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yashraj Upadhyay</title>
    <description>The latest articles on DEV Community by Yashraj Upadhyay (@yashraj_008).</description>
    <link>https://dev.to/yashraj_008</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1407633%2F91127c25-8689-4aa5-b22e-b0a36ca00d00.jpg</url>
      <title>DEV Community: Yashraj Upadhyay</title>
      <link>https://dev.to/yashraj_008</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yashraj_008"/>
    <language>en</language>
    <item>
      <title>Python dictionary interview questions</title>
      <dc:creator>Yashraj Upadhyay</dc:creator>
      <pubDate>Fri, 05 Apr 2024 16:58:00 +0000</pubDate>
      <link>https://dev.to/yashraj_008/python-dictionary-interview-questions-cgg</link>
      <guid>https://dev.to/yashraj_008/python-dictionary-interview-questions-cgg</guid>
      <description>&lt;h2&gt;
  
  
  Python dictionary is one of the widely used Data structure, which gives so much power in the developer's hand. Dictionary related questions are often asked in interviews. Here we discuss very popular questions which are mostly asked by interviewer.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Is python dictionary mutable?&lt;/strong&gt;&lt;br&gt;
Yes, Python dictionary is mutable as it stores data in key value pair.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to get the list of all keys and values in python dictionary?&lt;/strong&gt;&lt;br&gt;
In order to print all the keys, use dict.keys() method and for fetching all the values, use dict.values() method&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can we use tuple and list as the key of dictionary?&lt;/strong&gt;&lt;br&gt;
Immutable objects can be the key of dictionary, so if tuple contains only string, number or tuple, it can be used as a key of dictionary, while list is immutable datatype so it can't be used a dictionary key&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = {(1,2,3): 'Foo'} #correct
y = {(1,['a', 'b'], 2): 'Bar'} #error
z = {[1,2,3]: 'Foo'} #error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Whats is the use of dict.items() method?&lt;/strong&gt;&lt;br&gt;
It returns the value in form of list where tuple carry value of key and values&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = {'name': 'abc', 'age': 20}
l = list(x.items()) #output: [('name', 'abc'), ('age', 20)]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to use enumerate() function with dictionary?&lt;/strong&gt;&lt;br&gt;
enumerate() methods allow you to iterate over dictionary as well&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = {'name': 'abc', 'age': 20,'salary': 20000}
for key,val in enumerate(x):
    print(key,val)
#output:
0 name
1 age
2 salary
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What is the difference between del and pop() method in dictionary&lt;/strong&gt;&lt;br&gt;
One can simply delete the key from dictionary using del keyword by specifying the name of key. One of the drawback of using del keyword is that it will raise an key error if specified key will not exist in dictionary&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = {'name': 'abc', 'age': 20,'salary': 20000}
del x['name1']
#output
ERROR!
Traceback (most recent call last):
  File "&amp;lt;main.py&amp;gt;", line 2, in &amp;lt;module&amp;gt;
KeyError: 'name1'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;del x['name']
#output
{'age': 20, 'salary': 20000}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;pop() method allows to remove key value pair by simply passing key name inside pop method. One of the major advantage of using this method is if specified key is not exist, you can specify custom message, plus this method returns value as well which has been removed&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = {'name': 'abc', 'age': 20,'salary': 20000}
print(x.pop('age'))
print(x)
#Output
20
{'name': 'abc', 'salary': 20000}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = {'name': 'abc', 'age': 20,'salary': 20000}
print(x.pop('age1', 'key not found'))
print(x)
#Output
key not found
{'name': 'abc', 'age': 20, 'salary': 20000}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to use zip() method with dictionary&lt;/strong&gt;&lt;br&gt;
zip() method helps you to create a dictionary from two different list&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name = ['ajay', 'rohan', 'vikas']
salary = [1000, 2000, 3000]

print(dict(zip(name, salary)))
#output
{'ajay': 1000, 'rohan': 2000, 'vikas': 3000}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;How to use dictionary comprehension *&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name = ['ajay', 'rohan', 'vikas']
salary = [1000, 2000, 3000]

d = {key: val for key,val in zip(name, salary) if val &amp;gt; 1000}
print(d)
#output
{'rohan': 2000, 'vikas': 3000}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>dictionary</category>
      <category>interview</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
