

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codingeek</title>
	<atom:link href="https://www.codingeek.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codingeek.com</link>
	<description>A Coders Home</description>
	<lastBuildDate>Sat, 11 Mar 2023 08:33:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.6.17</generator>

<image>
	<url>https://www.codingeek.com/wp-content/uploads/2021/08/logo_size_invert.png</url>
	<title>Codingeek</title>
	<link>https://www.codingeek.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How do I check if a list is empty in Python?</title>
		<link>https://www.codingeek.com/python-examples/check-if-list-is-empty/</link>
					<comments>https://www.codingeek.com/python-examples/check-if-list-is-empty/#respond</comments>
		
		<dc:creator><![CDATA[Hitesh Garg]]></dc:creator>
		<pubDate>Tue, 14 Mar 2023 08:33:00 +0000</pubDate>
				<category><![CDATA[Python examples]]></category>
		<category><![CDATA[python 3]]></category>
		<category><![CDATA[python list examples]]></category>
		<guid isPermaLink="false">https://www.codingeek.com/?p=7222</guid>

					<description><![CDATA[<p>Often, we need to check if a list is empty . in this Python example we will discuss some o the ways to create a dictionary from separate lists in Python. Some of the topics which will be helpful for understanding the program implementation better are: List in Python Function in Python 1. Using the [&#8230;]</p>
<p>The post <a href="https://www.codingeek.com/python-examples/check-if-list-is-empty/">How do I check if a list is empty in Python?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Often, we need to check if a list is empty . in this <a href="http://codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener">Python example</a> we will discuss some o the ways to create a dictionary from separate lists in Python.</p>



<p>Some of the topics which will be helpful for understanding the program implementation better are:</p>



<ul><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener">List in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/function/?swcfpc=1" target="_blank" rel="noreferrer noopener">Function in Python</a></li></ul>



<h2>1. Using the len() function</h2>



<p>This function returns the number of elements in a list. If a list is empty, <code>len()</code> will return 0,</p>



<p>Now let&#8217;s implement a program to check if a list is empty.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fruits = []

if len(fruits) == 0:
  print("The list is empty")
else:
  print("The list is not empty")</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
The list is empty</pre>



<hr class="wp-block-separator"/>



<h2>2. Using the <code>not</code> operator</h2>



<p>The <code>not</code> operator is a logical operator that reverses the truth value of a boolean expression. If a list is empty, the boolean expression <code>not fruits</code> will evaluate to <code>True</code>.</p>



<p>Now lets implement the example again</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fruits = []

if not fruits:
  print("The list is empty")
else:
  print("The list is not empty")</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
The list is empty</pre>



<hr class="wp-block-separator"/>



<h2>3. Using the == operator</h2>



<p>This operator compares two values and returns <code>True</code> if they are equal, and <code>False</code> otherwise.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fruits = []

if fruits == []:
  print("The list is empty")
else:
  print("The list is not empty")</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
The list is empty</pre>



<hr class="wp-block-separator"/>



<h2>4. Using <code>bool()</code> operator</h2>



<p>The <code>bool()</code> function returns the boolean value of an object. If a list is empty, <code>bool(fruits)</code> will return <code>False</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fruits = []

if not bool(fruits):
  print("The list is empty")
else:
  print("The list is not empty")</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
The list is empty</pre>



<hr class="wp-block-separator"/>



<h2>5. Conclusion</h2>



<p>In this Python example, we discussed how to use the <code>len()</code> function, the not operator, the <code>==</code> operator, or the <code>bool()</code> function to check if a list is empty in Python.</p>



<hr class="wp-block-separator"/>



<p><strong>Helpful Links</strong></p>



<p>Please follow the <strong><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener">Python tutorial series</a></strong> or the <strong>menu in the sidebar</strong> for the complete tutorial series.</p>



<p>Also for examples in Python and practice please refer to <a href="https://www.codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener"><strong>Python Examples</strong></a>.</p>



<p>Complete code samples are present on <a href="https://github.com/HiteshGarg/codingeek/tree/master/Python" target="_blank" rel="noreferrer noopener">Github project</a>.</p>



<p><strong>Recommended Books</strong></p>



<a href="https://www.amazon.in/dp/0134692888?fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=7b91f6aa9d7b049597656cd5bd5817c5&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=0134692888&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=0134692888" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Automate-Boring-Stuff-Python-2nd/dp/1593279922?pd_rd_w=HfTHM&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=ETWXH8FK0CG38E458SP2&amp;pd_rd_r=2626052a-2870-4319-bab0-3a3aa2223acc&amp;pd_rd_wg=jwnfU&amp;pd_rd_i=1593279922&amp;psc=1&amp;fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ebcf34b169e248bb339c98b4ea19c821&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279922&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279922" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Python-Crash-Course-Eric-Matthes/dp/1593279280?pd_rd_w=PgvgO&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=Z1YZ0CRKV31V2646SJFC&amp;pd_rd_r=88dac74d-ed99-4d91-801d-094fabe7ae3c&amp;pd_rd_wg=T52yH&amp;pd_rd_i=1593279280&amp;psc=1&amp;fbclid=IwAR31g6aR1FSBvqWwmoBcop-XftKLyG8Ak6jKNGUKvxohTiVQ6Gt8fSKsyUc&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ff7918765640a1302a16c2eed995fb20&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279280&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279280" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">



<hr class="wp-block-separator"/>



<p><em><strong>An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding</strong></em></p>



<p><em><strong>Do not forget to share and Subscribe.</strong></em></p>



<p><em><strong>Happy coding!! </strong></em>?</p>



<p></p><p>The post <a href="https://www.codingeek.com/python-examples/check-if-list-is-empty/">How do I check if a list is empty in Python?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.codingeek.com/python-examples/check-if-list-is-empty/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How do I concatenate two lists in Python?</title>
		<link>https://www.codingeek.com/python-examples/concatenate-lists/</link>
					<comments>https://www.codingeek.com/python-examples/concatenate-lists/#respond</comments>
		
		<dc:creator><![CDATA[Hitesh Garg]]></dc:creator>
		<pubDate>Mon, 13 Mar 2023 08:33:00 +0000</pubDate>
				<category><![CDATA[Python examples]]></category>
		<category><![CDATA[python 3]]></category>
		<category><![CDATA[python list examples]]></category>
		<guid isPermaLink="false">https://www.codingeek.com/?p=7223</guid>

					<description><![CDATA[<p>In this Python example we will discuss some to concatenate two lists in Python. Some of the topics which will be helpful for understanding the program implementation better are: List in Python Function in Python Dictionary in Python For loop in Python 1. Using the + operator One way to concatenate two lists in Python [&#8230;]</p>
<p>The post <a href="https://www.codingeek.com/python-examples/concatenate-lists/">How do I concatenate two lists in Python?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In this <a href="http://codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener">Python example</a> we will discuss some to concatenate two lists in Python.</p>



<p>Some of the topics which will be helpful for understanding the program implementation better are:</p>



<ul><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener"><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener">List in Python</a></a></li><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener"><a href="https://www.codingeek.com/tutorials/python/function/?swcfpc=1" target="_blank" rel="noreferrer noopener">Function in Python</a></a></li><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener"><a href="https://www.codingeek.com/tutorials/python/python-data-types-dictionary-explanation-examples/" target="_blank" rel="noreferrer noopener">Dictionary in Python</a></a></li><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener"><a href="https://www.codingeek.com/tutorials/python/for-loop/" target="_blank" rel="noreferrer noopener">For loop in Python</a></a></li></ul>



<h2>1. Using the <code>+</code> operator</h2>



<p>One way to concatenate two lists in Python is to use the <code>+</code> operator. This operator combines the two lists into a new list that contains all the elements from both lists.</p>



<p>Here&#8217;s the code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">cars1 = ['Toyota', 'Honda', 'Nissan']
cars2 = ['BMW', 'Mercedes', 'Audi']

all_cars = cars1 + cars2
print(all_cars)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
['Toyota', 'Honda', 'Nissan', 'BMW', 'Mercedes', 'Audi']</pre>



<hr class="wp-block-separator"/>



<h2>2. Using the extend() method</h2>



<p><code>extend()</code> method adds all the elements from one list to another list. It modifies the original list and does not create a new list.</p>



<p>Here&#8217;s the code</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">cars1 = ['Toyota', 'Honda', 'Nissan']
cars2 = ['BMW', 'Mercedes', 'Audi']

cars1.extend(cars2)
print(cars1)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
['Toyota', 'Honda', 'Nissan', 'BMW', 'Mercedes', 'Audi']</pre>



<hr class="wp-block-separator"/>



<h2>3. Using the <code>*</code> operator</h2>



<p>The <code>*</code> operator creates a new list that contains multiple copies of the original list.</p>



<p>Here&#8217;s the code</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">cars1 = ['Toyota', 'Honda', 'Nissan']
cars2 = ['BMW', 'Mercedes', 'Audi']

all_cars = cars1 * 2 + cars2 * 3
print(all_cars)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
['Toyota', 'Honda', 'Nissan', 'Toyota', 'Honda', 'Nissan', 'BMW', 'Mercedes', 'Audi', 'BMW', 'Mercedes', 'Audi', 'BMW', 'Mercedes', 'Audi']</pre>



<hr class="wp-block-separator"/>



<h2>4. Conclusion</h2>



<p>In this Python example, we discussed how to concatenate two lists using the <code>+</code> operator, the <code>extend()</code> method, or the <code>*</code> operator.</p>



<hr class="wp-block-separator"/>



<p><strong>Helpful Links</strong></p>



<p>Please follow the <strong><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener">Python tutorial series</a></strong> or the <strong>menu in the sidebar</strong> for the complete tutorial series.</p>



<p>Also for examples in Python and practice please refer to <a href="https://www.codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener"><strong>Python Examples</strong></a>.</p>



<p>Complete code samples are present on <a href="https://github.com/HiteshGarg/codingeek/tree/master/Python" target="_blank" rel="noreferrer noopener">Github project</a>.</p>



<p><strong>Recommended Books</strong></p>



<a href="https://www.amazon.in/dp/0134692888?fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=7b91f6aa9d7b049597656cd5bd5817c5&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=0134692888&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=0134692888" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Automate-Boring-Stuff-Python-2nd/dp/1593279922?pd_rd_w=HfTHM&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=ETWXH8FK0CG38E458SP2&amp;pd_rd_r=2626052a-2870-4319-bab0-3a3aa2223acc&amp;pd_rd_wg=jwnfU&amp;pd_rd_i=1593279922&amp;psc=1&amp;fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ebcf34b169e248bb339c98b4ea19c821&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279922&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279922" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Python-Crash-Course-Eric-Matthes/dp/1593279280?pd_rd_w=PgvgO&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=Z1YZ0CRKV31V2646SJFC&amp;pd_rd_r=88dac74d-ed99-4d91-801d-094fabe7ae3c&amp;pd_rd_wg=T52yH&amp;pd_rd_i=1593279280&amp;psc=1&amp;fbclid=IwAR31g6aR1FSBvqWwmoBcop-XftKLyG8Ak6jKNGUKvxohTiVQ6Gt8fSKsyUc&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ff7918765640a1302a16c2eed995fb20&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279280&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279280" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">



<hr class="wp-block-separator"/>



<p><em><strong>An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding</strong></em></p>



<p><em><strong>Do not forget to share and Subscribe.</strong></em></p>



<p><em><strong>Happy coding!! </strong></em>?</p><p>The post <a href="https://www.codingeek.com/python-examples/concatenate-lists/">How do I concatenate two lists in Python?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.codingeek.com/python-examples/concatenate-lists/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to find the index of an item in a list in Python?</title>
		<link>https://www.codingeek.com/python-examples/find-index-of-item-in-list/</link>
					<comments>https://www.codingeek.com/python-examples/find-index-of-item-in-list/#respond</comments>
		
		<dc:creator><![CDATA[Hitesh Garg]]></dc:creator>
		<pubDate>Sun, 12 Mar 2023 08:33:00 +0000</pubDate>
				<category><![CDATA[Python examples]]></category>
		<category><![CDATA[python 3]]></category>
		<category><![CDATA[python list examples]]></category>
		<guid isPermaLink="false">https://www.codingeek.com/?p=7225</guid>

					<description><![CDATA[<p>To perform certain index-based operations while programming we need to access the index of an element in the list. In this Python example we will discuss some o the ways to find the index of an item in a list in Python. Some of the topics which will be helpful for understanding the program implementation [&#8230;]</p>
<p>The post <a href="https://www.codingeek.com/python-examples/find-index-of-item-in-list/">How to find the index of an item in a list in Python?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>To perform certain index-based operations while programming we need to access the index of an element in the list. In this <a href="http://codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener">Python example</a> we will discuss some o the ways to find the index of an item in a list in Python.</p>



<p>Some of the topics which will be helpful for understanding the program implementation better are:</p>



<ul><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener">List in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/tutorials/python/function/?swcfpc=1" target="_blank" rel="noreferrer noopener">Function in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/tutorials/python/python-data-types-dictionary-explanation-examples/" target="_blank" rel="noreferrer noopener">Dictionary in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/tutorials/python/for-loop/" target="_blank" rel="noreferrer noopener">For loop in Python</a></li></ul>



<h2>1. Using the <code>index()</code> method</h2>



<p>The <code>index()</code> method returns the index of the first occurrence of an item in a list and a ValueError is raised if the element is not found.</p>



<p>Now let&#8217;s implement a program to find the index of an item in a list in Python.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_list = [1, 2, 3, 4, 5]
item = 3
index = my_list.index(item)
print(index)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
2</pre>



<hr class="wp-block-separator"/>



<h2>2. Using enumerate() function</h2>



<p>The <code>enumerate()</code> function in Python takes an iterable as an argument and returns a tuple containing the index and the item at that index.</p>



<p>Here is an example.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_list = [1, 2, 3, 4, 5]
item = 3
for i, val in enumerate(my_list):
  if val == item:
    index = i
    break
print(index)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
2</pre>



<hr class="wp-block-separator"/>



<h2>3. Using the bisect module</h2>



<p>The <code>bisect</code> module in Python has <code>bisect_left()</code> function that can be used to find the index of an item in a sorted list.</p>



<p>Here is an example.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import bisect

my_list = [1, 2, 3, 4, 5]
item = 3
index = bisect.bisect_left(my_list, item)
print(index)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
2</pre>



<hr class="wp-block-separator"/>



<h2>4. Using <code>for</code> loop and <code>range()</code> function</h2>



<p>You can create a range of indices that matches the length of the list and iterate over them. Then, you can use the current index to access the corresponding item in the list.</p>



<p>Here is an example.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">countries = ['USA', 'Canada', 'UK', 'Germany', 'Japan']

for i in range(len(countries)):
  print(i, countries[i])</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
0 USA
1 Canada
2 UK
3 Germany
4 Japan</pre>



<hr class="wp-block-separator"/>



<h2>4. Conclusion</h2>



<p>In this Python example, we discussed the <code>index()</code> method, for loop, the <code>enumerate()</code> function, or the <code>bisect</code> module. to find the index of an item in a list in Python.</p>



<hr class="wp-block-separator"/>



<p><strong>Helpful Links</strong></p>



<p>Please follow the <strong><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener">Python tutorial series</a></strong> or the <strong>menu in the sidebar</strong> for the complete tutorial series.</p>



<p>Also for examples in Python and practice please refer to <a href="https://www.codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener"><strong>Python Examples</strong></a>.</p>



<p>Complete code samples are present on <a href="https://github.com/HiteshGarg/codingeek/tree/master/Python" target="_blank" rel="noreferrer noopener">Github project</a>.</p>



<p><strong>Recommended Books</strong></p>



<a href="https://www.amazon.in/dp/0134692888?fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=7b91f6aa9d7b049597656cd5bd5817c5&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=0134692888&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=0134692888" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Automate-Boring-Stuff-Python-2nd/dp/1593279922?pd_rd_w=HfTHM&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=ETWXH8FK0CG38E458SP2&amp;pd_rd_r=2626052a-2870-4319-bab0-3a3aa2223acc&amp;pd_rd_wg=jwnfU&amp;pd_rd_i=1593279922&amp;psc=1&amp;fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ebcf34b169e248bb339c98b4ea19c821&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279922&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279922" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Python-Crash-Course-Eric-Matthes/dp/1593279280?pd_rd_w=PgvgO&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=Z1YZ0CRKV31V2646SJFC&amp;pd_rd_r=88dac74d-ed99-4d91-801d-094fabe7ae3c&amp;pd_rd_wg=T52yH&amp;pd_rd_i=1593279280&amp;psc=1&amp;fbclid=IwAR31g6aR1FSBvqWwmoBcop-XftKLyG8Ak6jKNGUKvxohTiVQ6Gt8fSKsyUc&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ff7918765640a1302a16c2eed995fb20&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279280&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279280" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">



<hr class="wp-block-separator"/>



<p><em><strong>An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding</strong></em></p>



<p><em><strong>Do not forget to share and Subscribe.</strong></em></p>



<p><em><strong>Happy coding!! </strong></em>?</p><p>The post <a href="https://www.codingeek.com/python-examples/find-index-of-item-in-list/">How to find the index of an item in a list in Python?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.codingeek.com/python-examples/find-index-of-item-in-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to make a dictionary (dict) from separate lists of keys and values?</title>
		<link>https://www.codingeek.com/python-examples/how-to-make-a-dictionary-dict-from-separate-lists-of-keys-and-values/</link>
					<comments>https://www.codingeek.com/python-examples/how-to-make-a-dictionary-dict-from-separate-lists-of-keys-and-values/#respond</comments>
		
		<dc:creator><![CDATA[Hitesh Garg]]></dc:creator>
		<pubDate>Tue, 07 Mar 2023 12:00:00 +0000</pubDate>
				<category><![CDATA[Python examples]]></category>
		<category><![CDATA[python 3]]></category>
		<category><![CDATA[python dictionary examples]]></category>
		<guid isPermaLink="false">https://www.codingeek.com/?p=7193</guid>

					<description><![CDATA[<p>Often, we need to create a dictionary from separate lists of keys and values. in this Python example we will discuss some o the ways to create a dictionary from separate lists in Python. Some of the topics which will be helpful for understanding the program implementation better are: List in Python Function in Python [&#8230;]</p>
<p>The post <a href="https://www.codingeek.com/python-examples/how-to-make-a-dictionary-dict-from-separate-lists-of-keys-and-values/">How to make a dictionary (dict) from separate lists of keys and values?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Often, we need to create a <a href="https://www.codingeek.com/tutorials/python/python-data-types-dictionary-explanation-examples/" target="_blank" rel="noreferrer noopener">dictionary</a> from separate lists of keys and values. in this <a href="http://codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener">Python example</a> we will discuss some o the ways to create a dictionary from separate lists in Python.</p>



<p>Some of the topics which will be helpful for understanding the program implementation better are:</p>



<ul><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener">List in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/function/?swcfpc=1" target="_blank" rel="noreferrer noopener">Function in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/python-data-types-dictionary-explanation-examples/" target="_blank" rel="noopener" title="Python 3 Data Types – Dictionaries – Explanation with examples">Dictionary in Python</a></li></ul>



<h2>1. Using zip() Function</h2>



<p>One of the simplest ways to create a dictionary from separate lists is to use the <code>zip()</code> function. The <code>zip()</code> function returns an iterator that aggregates elements from each of the input iterables.</p>



<p>Now let&#8217;s implement a program to create a dictionary from 2 separate lists.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">keys = ['One', 'Two', 'Three']
values = [1, 2, 3]
my_dict = dict(zip(keys, values))
print(my_dict)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
{'One': 1, 'Two': 2, 'Three': 3} </pre>



<hr class="wp-block-separator"/>



<h2>2. Using a Dictionary Comprehension </h2>



<p>We can use a dictionary comprehension to iterate over the <code>keys</code> and <code>values</code> lists simultaneously and create a dictionary.</p>



<p>Now lets implement the example again</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">keys = ['One', 'Two', 'Three']
values = [1, 2, 3]
my_dict = {keys[i]: values[i] for i in range(len(keys))}
print(my_dict)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
{'One': 1, 'Two': 2, 'Three': 3} </pre>



<hr class="wp-block-separator"/>



<p><em>There are other ways like using a <code>for</code> loop to add elements to the empty dictionary but honestly, these are the better ways than implementing using a <code>for</code> loop. So we will skip that example for now, but I would still recommend you to write the example using <code>for</code> loop only. </em></p>



<hr class="wp-block-separator"/>



<h2>3. Conclusion</h2>



<p>In this Python example, we discussed multiple ways to create a dictionary from separate lists of keys and values.</p>



<hr class="wp-block-separator"/>



<p><strong>Helpful Links</strong></p>



<p>Please follow the <strong><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener">Python tutorial series</a></strong> or the <strong>menu in the sidebar</strong> for the complete tutorial series.</p>



<p>Also for examples in Python and practice please refer to <a href="https://www.codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener"><strong>Python Examples</strong></a>.</p>



<p>Complete code samples are present on <a href="https://github.com/HiteshGarg/codingeek/tree/master/Python" target="_blank" rel="noreferrer noopener">Github project</a>.</p>



<p><strong>Recommended Books</strong></p>



<a href="https://www.amazon.in/dp/0134692888?fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=7b91f6aa9d7b049597656cd5bd5817c5&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=0134692888&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=0134692888" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Automate-Boring-Stuff-Python-2nd/dp/1593279922?pd_rd_w=HfTHM&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=ETWXH8FK0CG38E458SP2&amp;pd_rd_r=2626052a-2870-4319-bab0-3a3aa2223acc&amp;pd_rd_wg=jwnfU&amp;pd_rd_i=1593279922&amp;psc=1&amp;fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ebcf34b169e248bb339c98b4ea19c821&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279922&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279922" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Python-Crash-Course-Eric-Matthes/dp/1593279280?pd_rd_w=PgvgO&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=Z1YZ0CRKV31V2646SJFC&amp;pd_rd_r=88dac74d-ed99-4d91-801d-094fabe7ae3c&amp;pd_rd_wg=T52yH&amp;pd_rd_i=1593279280&amp;psc=1&amp;fbclid=IwAR31g6aR1FSBvqWwmoBcop-XftKLyG8Ak6jKNGUKvxohTiVQ6Gt8fSKsyUc&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ff7918765640a1302a16c2eed995fb20&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279280&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279280" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">



<hr class="wp-block-separator"/>



<p><em><strong>An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding</strong></em></p>



<p><em><strong>Do not forget to share and Subscribe.</strong></em></p>



<p><em><strong>Happy coding!! </strong></em>?</p>



<p></p><p>The post <a href="https://www.codingeek.com/python-examples/how-to-make-a-dictionary-dict-from-separate-lists-of-keys-and-values/">How to make a dictionary (dict) from separate lists of keys and values?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.codingeek.com/python-examples/how-to-make-a-dictionary-dict-from-separate-lists-of-keys-and-values/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How do I make a flat list out of a list of lists in Python?</title>
		<link>https://www.codingeek.com/python-examples/flat-list-from-list-of-lists/</link>
					<comments>https://www.codingeek.com/python-examples/flat-list-from-list-of-lists/#respond</comments>
		
		<dc:creator><![CDATA[Hitesh Garg]]></dc:creator>
		<pubDate>Tue, 07 Mar 2023 08:33:04 +0000</pubDate>
				<category><![CDATA[Python examples]]></category>
		<category><![CDATA[python 3]]></category>
		<category><![CDATA[python list examples]]></category>
		<guid isPermaLink="false">https://www.codingeek.com/?p=7226</guid>

					<description><![CDATA[<p>Often, we end with a list of lists while programming and need to process every element or need a flat list out of it. in this Python example we will discuss some of the ways to make a flat list out of a list of lists in Python. Some of the topics which will be [&#8230;]</p>
<p>The post <a href="https://www.codingeek.com/python-examples/flat-list-from-list-of-lists/">How do I make a flat list out of a list of lists in Python?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Often, we end with a list of lists while programming and need to process every element or need a flat list out of it. in this <a href="http://codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener">Python example</a> we will discuss some of the ways to make a flat list out of a list of lists in Python.</p>



<p>Some of the topics which will be helpful for understanding the program implementation better are:</p>



<ul><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener">List in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/function/?swcfpc=1" target="_blank" rel="noreferrer noopener">Function in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/python-data-types-dictionary-explanation-examples/" target="_blank" rel="noopener" title="Python 3 Data Types – Dictionaries – Explanation with examples">Dictionary in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/for-loop/" target="_blank" rel="noopener" title="Python for loop">For loop in Python</a></li></ul>



<h2>1. Using nested loops</h2>



<p>One way to flatten a list of lists is to use nested loops. You can iterate over each element of the outer list, and for each element, iterate over its sub-list and append each item to a new list.</p>



<p>Now let&#8217;s implement a program to make a flat list out of a list of lists in Python</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">nested_list = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
flat_list = []
for sublist in nested_list:
  for item in sublist:
    flat_list.append(item)
print(flat_list)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
[1, 2, 3, 4, 5, 6, 7, 8, 9]</pre>



<hr class="wp-block-separator"/>



<h2>2. Using list comprehension</h2>



<p>The same can be achieved via list comprehension i.e. flatten out list in a single statement.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">nested_list = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
flat_list = [item for sublist in nested_list for item in sublist]
print(flat_list)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
[1, 2, 3, 4, 5, 6, 7, 8, 9]</pre>



<hr class="wp-block-separator"/>



<h2>3. Using itertools.chain</h2>



<p>The <code>itertools</code> module in Python provides a <code>chain()</code> function that takes multiple <code>iterables</code> as arguments and returns a single iterable that iterates over each item of the input <code>iterables</code>.</p>



<p>Here is the example.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import itertools

nested_list = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
flat_list = list(itertools.chain(*nested_list))
print(flat_list)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
[1, 2, 3, 4, 5, 6, 7, 8, 9]</pre>



<h2>4. Using the functools.reduce() function</h2>



<p>The reduce() function in Python&#8217;s functools module takes a function and a sequence as arguments and applies the function cumulatively to the items of the sequence, from left to right. Here is the code.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import functools

nested_list = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
flat_list = functools.reduce(lambda x, y: x+y, nested_list)
print(flat_list)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
[1, 2, 3, 4, 5, 6, 7, 8, 9]</pre>



<hr class="wp-block-separator"/>



<h2>5. Conclusion</h2>



<p>In this Python example, we discussed multiple ways to make a flat list out of a list of lists in Python.</p>



<hr class="wp-block-separator"/>



<p><strong>Helpful Links</strong></p>



<p>Please follow the <strong><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener">Python tutorial series</a></strong> or the <strong>menu in the sidebar</strong> for the complete tutorial series.</p>



<p>Also for examples in Python and practice please refer to <a href="https://www.codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener"><strong>Python Examples</strong></a>.</p>



<p>Complete code samples are present on <a href="https://github.com/HiteshGarg/codingeek/tree/master/Python" target="_blank" rel="noreferrer noopener">Github project</a>.</p>



<p><strong>Recommended Books</strong></p>



<a href="https://www.amazon.in/dp/0134692888?fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=7b91f6aa9d7b049597656cd5bd5817c5&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=0134692888&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=0134692888" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Automate-Boring-Stuff-Python-2nd/dp/1593279922?pd_rd_w=HfTHM&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=ETWXH8FK0CG38E458SP2&amp;pd_rd_r=2626052a-2870-4319-bab0-3a3aa2223acc&amp;pd_rd_wg=jwnfU&amp;pd_rd_i=1593279922&amp;psc=1&amp;fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ebcf34b169e248bb339c98b4ea19c821&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279922&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279922" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Python-Crash-Course-Eric-Matthes/dp/1593279280?pd_rd_w=PgvgO&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=Z1YZ0CRKV31V2646SJFC&amp;pd_rd_r=88dac74d-ed99-4d91-801d-094fabe7ae3c&amp;pd_rd_wg=T52yH&amp;pd_rd_i=1593279280&amp;psc=1&amp;fbclid=IwAR31g6aR1FSBvqWwmoBcop-XftKLyG8Ak6jKNGUKvxohTiVQ6Gt8fSKsyUc&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ff7918765640a1302a16c2eed995fb20&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279280&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279280" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">



<hr class="wp-block-separator"/>



<p><em><strong>An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding</strong></em></p>



<p><em><strong>Do not forget to share and Subscribe.</strong></em></p>



<p><em><strong>Happy coding!! </strong></em>?</p>



<p></p><p>The post <a href="https://www.codingeek.com/python-examples/flat-list-from-list-of-lists/">How do I make a flat list out of a list of lists in Python?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.codingeek.com/python-examples/flat-list-from-list-of-lists/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Differences between dict.get(key) and dict[key] and which one to use?</title>
		<link>https://www.codingeek.com/python-examples/dict-get-vs-dict-key/</link>
					<comments>https://www.codingeek.com/python-examples/dict-get-vs-dict-key/#respond</comments>
		
		<dc:creator><![CDATA[Hitesh Garg]]></dc:creator>
		<pubDate>Mon, 06 Mar 2023 12:00:00 +0000</pubDate>
				<category><![CDATA[Python examples]]></category>
		<category><![CDATA[python 3]]></category>
		<category><![CDATA[python dictionary examples]]></category>
		<guid isPermaLink="false">https://www.codingeek.com/?p=7192</guid>

					<description><![CDATA[<p>When working with dictionaries, there are two ways to retrieve the value associated with a particular key: using the dict[key] syntax or using the dict.get(key) method. In this Python example, we will discuss the differences between these two approaches and analyse which one to use and when. Some of the topics which will be helpful [&#8230;]</p>
<p>The post <a href="https://www.codingeek.com/python-examples/dict-get-vs-dict-key/">Differences between dict.get(key) and dict[key] and which one to use?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>When working with <a href="https://www.codingeek.com/tutorials/python/python-data-types-dictionary-explanation-examples/" target="_blank" rel="noopener" title="">dictionaries</a>, there are two ways to retrieve the value associated with a particular key: using the <code>dict[key]</code> syntax or using the <code>dict.get(key)</code> method. In this <a href="http://codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener">Python example</a>, we will discuss the differences between these two approaches and analyse which one to use and when.</p>



<p>Some of the topics which will be helpful for understanding the program implementation better are:</p>



<ul><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener">List in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/function/?swcfpc=1" target="_blank" rel="noreferrer noopener">Function in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/python-data-types-dictionary-explanation-examples/" target="_blank" rel="noopener" title="Python 3 Data Types – Dictionaries – Explanation with examples">Dictionary in Python</a></li></ul>



<h2>1. Retrieving Values with <code>dict[key]</code></h2>



<p>The <code>dict[key]</code> syntax retrieves the value associated with a given key in a dictionary. For example:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_dict = {'key1': 'value1', 'key2': 'value2'}
value = my_dict['key1']
print(value)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
'value1'</pre>



<p>If the key does not exist in the dictionary, this will raise a <code>KeyError</code>. To avoid this, we can use the <code>in</code> keyword to check if the key exists in the dictionary first:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_dict = {'key1': 'value1', 'key2': 'value2'}
if 'key3' in my_dict:
  value = my_dict['key3']
else:
  value = None
print(value)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
None</pre>



<hr class="wp-block-separator"/>



<h2>2. Retrieving Values with <code>dict.get(key)</code></h2>



<p>With <code>dict(key)</code> we do not need to care about whether the key exists in the dictionary or not. If the key does not exist in the dictionary, <code>dict.get(key)</code> returns <code>None</code> instead of raising a <code>KeyError</code>. For example:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_dict = {'key1': 'value1', 'key2': 'value2'}
value = my_dict.get('key3')
print(value)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
None</pre>



<p>In addition to that we can also specify a default value to return if the key does not exist in the dictionary:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_dict = {'key1': 'value1', 'key2': 'value2'}
value = my_dict.get('key3', 'codingeek')
print(value)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
codingeek</pre>



<hr class="wp-block-separator"/>



<h2>3. Conclusion</h2>



<p>In summary, both <code>dict[key]</code> and <code>dict.get(key)</code> are useful approaches for retrieving values from dictionaries in Python. In general, using <code>dict[key]</code> is preferred when you know that the key exists in the dictionary and you want to retrieve its value. However, if you&#8217;re not sure whether the key exists in the dictionary or you want to handle the case where it doesn&#8217;t exist, <code>dict.get(key)</code> is a safer choice.</p>



<hr class="wp-block-separator"/>



<p><strong>Helpful Links</strong></p>



<p>Please follow the <strong><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener">Python tutorial series</a></strong> or the <strong>menu in the sidebar</strong> for the complete tutorial series.</p>



<p>Also for examples in Python and practice please refer to <a href="https://www.codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener"><strong>Python Examples</strong></a>.</p>



<p>Complete code samples are present on <a href="https://github.com/HiteshGarg/codingeek/tree/master/Python" target="_blank" rel="noreferrer noopener">Github project</a>.</p>



<p><strong>Recommended Books</strong></p>



<a href="https://www.amazon.in/dp/0134692888?fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=7b91f6aa9d7b049597656cd5bd5817c5&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=0134692888&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=0134692888" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Automate-Boring-Stuff-Python-2nd/dp/1593279922?pd_rd_w=HfTHM&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=ETWXH8FK0CG38E458SP2&amp;pd_rd_r=2626052a-2870-4319-bab0-3a3aa2223acc&amp;pd_rd_wg=jwnfU&amp;pd_rd_i=1593279922&amp;psc=1&amp;fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ebcf34b169e248bb339c98b4ea19c821&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279922&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279922" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Python-Crash-Course-Eric-Matthes/dp/1593279280?pd_rd_w=PgvgO&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=Z1YZ0CRKV31V2646SJFC&amp;pd_rd_r=88dac74d-ed99-4d91-801d-094fabe7ae3c&amp;pd_rd_wg=T52yH&amp;pd_rd_i=1593279280&amp;psc=1&amp;fbclid=IwAR31g6aR1FSBvqWwmoBcop-XftKLyG8Ak6jKNGUKvxohTiVQ6Gt8fSKsyUc&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ff7918765640a1302a16c2eed995fb20&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279280&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279280" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">



<hr class="wp-block-separator"/>



<p><em><strong>An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding</strong></em></p>



<p><em><strong>Do not forget to share and Subscribe.</strong></em></p>



<p><em><strong>Happy coding!! </strong></em>?</p>



<p></p><p>The post <a href="https://www.codingeek.com/python-examples/dict-get-vs-dict-key/">Differences between dict.get(key) and dict[key] and which one to use?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.codingeek.com/python-examples/dict-get-vs-dict-key/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Count the frequencies in a list using a dictionary in Python?</title>
		<link>https://www.codingeek.com/python-examples/count-frequencies-in-list-using-dictionary/</link>
					<comments>https://www.codingeek.com/python-examples/count-frequencies-in-list-using-dictionary/#respond</comments>
		
		<dc:creator><![CDATA[Hitesh Garg]]></dc:creator>
		<pubDate>Sun, 05 Mar 2023 12:18:08 +0000</pubDate>
				<category><![CDATA[Python examples]]></category>
		<category><![CDATA[python 3]]></category>
		<category><![CDATA[python dictionary examples]]></category>
		<guid isPermaLink="false">https://www.codingeek.com/?p=7194</guid>

					<description><![CDATA[<p>A dictionary can be used to count the frequencies of elements in the list. Python example we will discuss how to count the frequencies in a list using a dictionary in Python. Some of the topics which will be helpful for understanding the program implementation better are: List in Python Function in Python Dictionary in [&#8230;]</p>
<p>The post <a href="https://www.codingeek.com/python-examples/count-frequencies-in-list-using-dictionary/">How to Count the frequencies in a list using a dictionary in Python?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>A <a href="https://www.codingeek.com/tutorials/python/python-data-types-dictionary-explanation-examples/" target="_blank" rel="noopener" title="">dictionary</a> can be used to count the frequencies of elements in the list. <a href="http://codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener">Python example</a> we will discuss how to count the frequencies in a list using a dictionary in Python.</p>



<p>Some of the topics which will be helpful for understanding the program implementation better are:</p>



<ul><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener">List in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/function/?swcfpc=1" target="_blank" rel="noreferrer noopener">Function in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/python-data-types-dictionary-explanation-examples/" target="_blank" rel="noopener" title="Python 3 Data Types – Dictionaries – Explanation with examples">Dictionary in Python</a></li></ul>



<h2>1. Counting Frequencies Using a Dictionary</h2>



<p>The keys in a dictionary must be unique, while values can be duplicated. We can use this property to count the frequencies of elements in a list by treating each element in the list as a key in a dictionary and incrementing its value for each occurrence.</p>



<p>Now let&#8217;s implement a program to count the frequencies of the elements in Python.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'banana', 'kiwi', 'apple']
fruit_freq = {}

for fruit in fruits:
  if fruit in fruit_freq:
    fruit_freq[fruit] += 1
  else:
    fruit_freq[fruit] = 1

print(fruit_freq)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
{'apple': 3, 'banana': 3, 'orange': 1, 'kiwi': 1}</pre>



<p>For each fruit, we check if it is already a key in <code>fruit_freq</code>. If it is, we increment its value by 1 otherwise we add it to <code>fruit_freq</code> with a value of 1. Finally, we print <code>fruit_freq</code> to verify that the frequencies have been counted correctly.</p>



<hr class="wp-block-separator"/>



<h2>2. Using the <code>collections</code> Module</h2>



<p>The <code>collections</code> module in Python provides a built-in <code>Counter</code> class that can be used to count the frequencies of elements in a list. The <code>Counter</code> object automatically counts the frequencies of each element in the list.</p>



<p>Here&#8217;s an example:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from collections import Counter

fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'banana', 'kiwi', 'apple']
fruit_freq = Counter(fruits)

print(fruit_freq)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
Counter({'apple': 3, 'banana': 3, 'orange': 1, 'kiwi': 1})</pre>



<hr class="wp-block-separator"/>



<h2>3. Conclusion</h2>



<p>In this Python example, we discussed multiple ways to count the frequencies in a list using a dictionary, one with a custom implementation and another using the Counter class. </p>



<hr class="wp-block-separator"/>



<p><strong>Helpful Links</strong></p>



<p>Please follow the <strong><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener">Python tutorial series</a></strong> or the <strong>menu in the sidebar</strong> for the complete tutorial series.</p>



<p>Also for examples in Python and practice please refer to <a href="https://www.codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener"><strong>Python Examples</strong></a>.</p>



<p>Complete code samples are present on <a href="https://github.com/HiteshGarg/codingeek/tree/master/Python" target="_blank" rel="noreferrer noopener">Github project</a>.</p>



<p><strong>Recommended Books</strong></p>



<a href="https://www.amazon.in/dp/0134692888?fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=7b91f6aa9d7b049597656cd5bd5817c5&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=0134692888&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=0134692888" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Automate-Boring-Stuff-Python-2nd/dp/1593279922?pd_rd_w=HfTHM&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=ETWXH8FK0CG38E458SP2&amp;pd_rd_r=2626052a-2870-4319-bab0-3a3aa2223acc&amp;pd_rd_wg=jwnfU&amp;pd_rd_i=1593279922&amp;psc=1&amp;fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ebcf34b169e248bb339c98b4ea19c821&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279922&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279922" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Python-Crash-Course-Eric-Matthes/dp/1593279280?pd_rd_w=PgvgO&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=Z1YZ0CRKV31V2646SJFC&amp;pd_rd_r=88dac74d-ed99-4d91-801d-094fabe7ae3c&amp;pd_rd_wg=T52yH&amp;pd_rd_i=1593279280&amp;psc=1&amp;fbclid=IwAR31g6aR1FSBvqWwmoBcop-XftKLyG8Ak6jKNGUKvxohTiVQ6Gt8fSKsyUc&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ff7918765640a1302a16c2eed995fb20&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279280&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279280" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">



<hr class="wp-block-separator"/>



<p><em><strong>An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding</strong></em></p>



<p><em><strong>Do not forget to share and Subscribe.</strong></em></p>



<p><em><strong>Happy coding!! </strong></em>?</p>



<p></p><p>The post <a href="https://www.codingeek.com/python-examples/count-frequencies-in-list-using-dictionary/">How to Count the frequencies in a list using a dictionary in Python?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.codingeek.com/python-examples/count-frequencies-in-list-using-dictionary/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Convert a list of Tuples into Dictionary in Python?</title>
		<link>https://www.codingeek.com/python-examples/convert-list-of-tuples-into-dictionary/</link>
					<comments>https://www.codingeek.com/python-examples/convert-list-of-tuples-into-dictionary/#respond</comments>
		
		<dc:creator><![CDATA[Hitesh Garg]]></dc:creator>
		<pubDate>Fri, 03 Mar 2023 18:37:00 +0000</pubDate>
				<category><![CDATA[Python examples]]></category>
		<category><![CDATA[python 3]]></category>
		<category><![CDATA[python dictionary examples]]></category>
		<guid isPermaLink="false">https://www.codingeek.com/?p=7196</guid>

					<description><![CDATA[<p>A dictionary in Python is a collection of key-value pairs. Sometimes we may have a list of tuples, where each tuple represents a key-value pair, and we may want to convert it into a dictionary. In this Python example, we will discuss how to convert a list of tuples. Some of the topics which will [&#8230;]</p>
<p>The post <a href="https://www.codingeek.com/python-examples/convert-list-of-tuples-into-dictionary/">How to Convert a list of Tuples into Dictionary in Python?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>A <a href="https://www.codingeek.com/tutorials/python/python-data-types-dictionary-explanation-examples/" target="_blank" rel="noopener" title="">dictionary</a> in Python is a collection of key-value pairs. Sometimes we may have a list of tuples, where each tuple represents a key-value pair, and we may want to convert it into a dictionary. In this <a href="http://codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener">Python example</a>, we will discuss how to convert a list of tuples.</p>



<p>Some of the topics which will be helpful for understanding the program implementation better are:</p>



<ul><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener">List in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/function/?swcfpc=1" target="_blank" rel="noreferrer noopener">Function in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/python-data-types-dictionary-explanation-examples/" target="_blank" rel="noopener" title="Python 3 Data Types – Dictionaries – Explanation with examples">Dictionary in Python</a></li></ul>



<pre class="wp-block-preformatted hit-code-sample"><strong>Example</strong>:

<strong>Input :</strong>  
people = [('John', 25), ('Jane', 30), ('Mike', 40)] 
<strong>Output :</strong> 
{
 "John" : 25,
 "Jane" : 30,
 "Mike" : 40
}</pre>



<h2>1. Using a <code>for</code> loop</h2>



<p>We can create an empty dictionary and iterate through the list of tuples, adding each tuple to the dictionary as a key-value pair.</p>



<p>Now let&#8217;s implement a program to convert list of tuples to a dictionary using <code>for</code> loop.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Using a for loop
people_dict = {}
people = [('John', 25), ('Jane', 30), ('Mike', 40)] 
for person in people:
    people_dict[person[0]] = person[1]
print(people_dict)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
{'John': 25, 'Jane': 30, 'Mike': 40}</pre>



<hr class="wp-block-separator"/>



<h2>2. Using <code>dict</code> constructor</h2>



<p>This is a one-line implementation of the previous example. We will use list comprehension to iterate through the tuples and create a dictionary using the <code>dict</code> constructor</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Using dictionary comprehension
people = [('John', 25), ('Jane', 30), ('Mike', 40)] 
people_dict = dict((person[0], person[1]) for person in people)
print(people_dict)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
{'John': 25, 'Jane': 30, 'Mike': 40}</pre>



<hr class="wp-block-separator"/>



<h2>3. Using the <code>zip()</code> function</h2>



<p>We can use <code>zip()</code> to combine the first elements of tuples into a list, and the second element of the tuples into a list, and then pass these lists to the <code>dict()</code> constructor.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Using the zip() function
people = [('John', 25), ('Jane', 30), ('Mike', 40)] 
keys, values = zip(*people)
people_dict = dict(zip(keys, values))
print(people_dict)
</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
{'John': 25, 'Jane': 30, 'Mike': 40}</pre>



<hr class="wp-block-separator"/>



<h2>4. Conclusion</h2>



<p>In this Python example, we have explored three different methods to convert a list of tuples into a dictionary in Python. </p>



<hr class="wp-block-separator"/>



<p><strong>Helpful Links</strong></p>



<p>Please follow the <strong><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener">Python tutorial series</a></strong> or the <strong>menu in the sidebar</strong> for the complete tutorial series.</p>



<p>Also for examples in Python and practice please refer to <a href="https://www.codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener"><strong>Python Examples</strong></a>.</p>



<p>Complete code samples are present on <a href="https://github.com/HiteshGarg/codingeek/tree/master/Python" target="_blank" rel="noreferrer noopener">Github project</a>.</p>



<p><strong>Recommended Books</strong></p>



<a href="https://www.amazon.in/dp/0134692888?fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=7b91f6aa9d7b049597656cd5bd5817c5&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=0134692888&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=0134692888" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Automate-Boring-Stuff-Python-2nd/dp/1593279922?pd_rd_w=HfTHM&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=ETWXH8FK0CG38E458SP2&amp;pd_rd_r=2626052a-2870-4319-bab0-3a3aa2223acc&amp;pd_rd_wg=jwnfU&amp;pd_rd_i=1593279922&amp;psc=1&amp;fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ebcf34b169e248bb339c98b4ea19c821&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279922&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279922" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Python-Crash-Course-Eric-Matthes/dp/1593279280?pd_rd_w=PgvgO&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=Z1YZ0CRKV31V2646SJFC&amp;pd_rd_r=88dac74d-ed99-4d91-801d-094fabe7ae3c&amp;pd_rd_wg=T52yH&amp;pd_rd_i=1593279280&amp;psc=1&amp;fbclid=IwAR31g6aR1FSBvqWwmoBcop-XftKLyG8Ak6jKNGUKvxohTiVQ6Gt8fSKsyUc&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ff7918765640a1302a16c2eed995fb20&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279280&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279280" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">



<hr class="wp-block-separator"/>



<p><em><strong>An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding</strong></em></p>



<p><em><strong>Do not forget to share and Subscribe.</strong></em></p>



<p><em><strong>Happy coding!! </strong></em>?</p>



<p></p><p>The post <a href="https://www.codingeek.com/python-examples/convert-list-of-tuples-into-dictionary/">How to Convert a list of Tuples into Dictionary in Python?</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.codingeek.com/python-examples/convert-list-of-tuples-into-dictionary/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Merge Two Dictionaries in Python</title>
		<link>https://www.codingeek.com/python-examples/merge-two-dictionaries/</link>
					<comments>https://www.codingeek.com/python-examples/merge-two-dictionaries/#respond</comments>
		
		<dc:creator><![CDATA[Hitesh Garg]]></dc:creator>
		<pubDate>Thu, 02 Mar 2023 12:00:00 +0000</pubDate>
				<category><![CDATA[Python examples]]></category>
		<category><![CDATA[python 3]]></category>
		<category><![CDATA[python dictionary examples]]></category>
		<guid isPermaLink="false">https://www.codingeek.com/?p=7180</guid>

					<description><![CDATA[<p>In this Python example we will discuss some of the ways to merge two dictionaries as this is one of the most common operations in Python. Some of the topics which will be helpful for understanding the program implementation better are: List in Python Function in Python Dictionary in Python 1. Using the update Method [&#8230;]</p>
<p>The post <a href="https://www.codingeek.com/python-examples/merge-two-dictionaries/">How to Merge Two Dictionaries in Python</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In this <a href="http://codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener">Python example</a> we will discuss some of the ways to merge two dictionaries as this is one of the most common operations in Python.</p>



<p>Some of the topics which will be helpful for understanding the program implementation better are:</p>



<ul><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener">List in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/function/?swcfpc=1" target="_blank" rel="noreferrer noopener">Function in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/python-data-types-dictionary-explanation-examples/" target="_blank" rel="noopener" title="Python 3 Data Types – Dictionaries – Explanation with examples">Dictionary in Python</a></li></ul>



<h2>1. Using the <code>update</code> Method</h2>



<p>One of the easiest way to merge the dictionaries is to use <code>update</code> method. This method updates the first dictionary with the key-value pairs from the second dictionary. </p>



<p>Now let&#8217;s implement a program to merge dictionaries using <code>update</code> method. After emerging the result dictionary will have all the key values from both the initial dictionaries.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}

dict1.update(dict2)

print(dict1)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
{'a': 1, 'b': 2, 'c': 3, 'd': 4}</pre>



<hr class="wp-block-separator"/>



<h2>2. Using Dictionary Unpacking (<code>**</code>) Operator </h2>



<p>Another simple way is to use dictionary unpacking operator. This operator unpacks the key-value pairs from one dictionary and adds them to another dictionary. </p>



<p>Now let&#8217;s implement a program to merge dictionaries using dictionary unpacking operator operator.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}

merged_dict = {**dict1, **dict2}

print(merged_dict)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
{'a': 1, 'b': 2, 'c': 3, 'd': 4}</pre>



<hr class="wp-block-separator"/>



<h2>3. Using the <code>dict</code> Constructor</h2>



<p>The <code>dict</code> constructor can also be used to merge two dictionaries in Python. This method creates a new dictionary by combining the key-value pairs from two dictionaries.</p>



<p>Now let&#8217;s implement a program to merge dictionaries using <code>dict</code> constructor.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}

merged_dict = dict(dict1, **dict2)

print(merged_dict)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
{'a': 1, 'b': 2, 'c': 3, 'd': 4}</pre>



<hr class="wp-block-separator"/>



<h2>4. Using the <code>ChainMap</code> Function</h2>



<p>The <code>ChainMap</code> function from the <code>collections</code> module can also be used to merge two dictionaries in Python. This function creates a view of all the dictionaries in a list and allows you to access their key-value pairs as if they were a single dictionary.</p>



<p>Now let&#8217;s implement a program to merge dictionaries using <code>ChainMap</code> function.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from collections import ChainMap

dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}

merged_dict = ChainMap(dict1, dict2)

print(merged_dict)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
ChainMap({'a': 1, 'b': 2}, {'c': 3, 'd': 4})</pre>



<hr class="wp-block-separator"/>



<h2>5. Using the <code>merge</code> Method</h2>



<p>The <code>merge</code> method from the <code>dict</code> class creates a new dictionary by combining the key-value pairs from two dictionaries.</p>



<p>Now let&#8217;s implement a program to merge dictionaries using <code>merge</code> method.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}

merged_dict = dict1.copy()
merged_dict.update(dict2)

print(merged_dict)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
{'a': 1, 'b': 2, 'c': 3, 'd': 4}</pre>



<hr class="wp-block-separator"/>



<h2>4. Conclusion</h2>



<p>In this Python example, we discussed multiple ways to merge two dictionaries. We explored five different methods for merging dictionaries, including using the <code>update</code> method, the <code>**</code> operator, the <code>dict</code> constructor, the <code>ChainMap</code> function, and the <code>merge</code> method.</p>



<hr class="wp-block-separator"/>



<p><strong>Helpful Links</strong></p>



<p>Please follow the <strong><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener">Python tutorial series</a></strong> or the <strong>menu in the sidebar</strong> for the complete tutorial series.</p>



<p>Also for examples in Python and practice please refer to <a href="https://www.codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener"><strong>Python Examples</strong></a>.</p>



<p>Complete code samples are present on <a href="https://github.com/HiteshGarg/codingeek/tree/master/Python" target="_blank" rel="noreferrer noopener">Github project</a>.</p>



<p><strong>Recommended Books</strong></p>



<a href="https://www.amazon.in/dp/0134692888?fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=7b91f6aa9d7b049597656cd5bd5817c5&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=0134692888&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=0134692888" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Automate-Boring-Stuff-Python-2nd/dp/1593279922?pd_rd_w=HfTHM&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=ETWXH8FK0CG38E458SP2&amp;pd_rd_r=2626052a-2870-4319-bab0-3a3aa2223acc&amp;pd_rd_wg=jwnfU&amp;pd_rd_i=1593279922&amp;psc=1&amp;fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ebcf34b169e248bb339c98b4ea19c821&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279922&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279922" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Python-Crash-Course-Eric-Matthes/dp/1593279280?pd_rd_w=PgvgO&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=Z1YZ0CRKV31V2646SJFC&amp;pd_rd_r=88dac74d-ed99-4d91-801d-094fabe7ae3c&amp;pd_rd_wg=T52yH&amp;pd_rd_i=1593279280&amp;psc=1&amp;fbclid=IwAR31g6aR1FSBvqWwmoBcop-XftKLyG8Ak6jKNGUKvxohTiVQ6Gt8fSKsyUc&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ff7918765640a1302a16c2eed995fb20&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279280&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279280" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">



<hr class="wp-block-separator"/>



<p><em><strong>An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding</strong></em></p>



<p><em><strong>Do not forget to share and Subscribe.</strong></em></p>



<p><em><strong>Happy coding!! </strong></em>?</p>



<p></p><p>The post <a href="https://www.codingeek.com/python-examples/merge-two-dictionaries/">How to Merge Two Dictionaries in Python</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.codingeek.com/python-examples/merge-two-dictionaries/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Given a dictionary and a character array, print all valid words that are possible in Python</title>
		<link>https://www.codingeek.com/python-examples/print-all-valid-possible-words/</link>
					<comments>https://www.codingeek.com/python-examples/print-all-valid-possible-words/#respond</comments>
		
		<dc:creator><![CDATA[Hitesh Garg]]></dc:creator>
		<pubDate>Wed, 01 Mar 2023 16:00:00 +0000</pubDate>
				<category><![CDATA[Python examples]]></category>
		<category><![CDATA[python 3]]></category>
		<category><![CDATA[python dictionary examples]]></category>
		<guid isPermaLink="false">https://www.codingeek.com/?p=7195</guid>

					<description><![CDATA[<p>In this Python example we will discuss the following problem statement. Given a dictionary of words and a character array, write a function that prints all valid words that are possible using characters from the array. A word is considered valid if it can be formed using only the characters from the array, and if [&#8230;]</p>
<p>The post <a href="https://www.codingeek.com/python-examples/print-all-valid-possible-words/">Given a dictionary and a character array, print all valid words that are possible in Python</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In this <a href="http://codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener">Python example</a> we will discuss the following problem statement. Given a dictionary of words and a character array, write a function that prints all valid words that are possible using characters from the array. A word is considered valid if it can be formed using only the characters from the array, and if it is present in the dictionary.</p>



<p>Some of the topics which will be helpful for understanding the program implementation better are:</p>



<ul><li><a href="https://www.codingeek.com/tutorials/python/list/?swcfpc=1" target="_blank" rel="noreferrer noopener">List in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/function/?swcfpc=1" target="_blank" rel="noreferrer noopener">Function in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/python-data-types-dictionary-explanation-examples/" target="_blank" rel="noopener" title="Python 3 Data Types – Dictionaries – Explanation with examples">Dictionary in Python</a></li><li><a href="https://www.codingeek.com/tutorials/python/for-loop/" target="_blank" rel="noopener" title="Python for loop">For loop in Python</a></li></ul>



<h2>1. Using a for loop</h2>



<p>We can iterate through each word in the dictionary and check if it can be formed using only the characters in the <code>char_array</code>. To check if a word can be formed using the characters in the array, we can convert the word and the array into sets and use the <code>issubset()</code> method to check if that is the subset of the character array.</p>



<p>Now let&#8217;s implement a program for this problem &#8211;</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def print_valid_words(dictionary, char_array):
  for word in dictionary:
    if set(word).issubset(set(char_array)):
      print(word)

dictionary = {'cat', 'dog', 'act', 'god', 'bat'}
char_array = ['a', 'c', 't', 'o', 'g', 'd']
print_valid_words(dictionary, char_array)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
cat
god
dog
act</pre>



<hr class="wp-block-separator"/>



<h2>2. Using list comprehension</h2>



<p>This is exactly like the previous example but instead of using a for loop we will use list comprehension to achieve the same.</p>



<p>Now let&#8217;s implement a program for this problem &#8211;</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def print_valid_words(dictionary, char_array):
  valid_words = [word for word in dictionary if set(word).issubset(set(char_array))]
  print(valid_words)

dictionary = {'cat', 'dog', 'act', 'god', 'bat'}
char_array = ['a', 'c', 't', 'o', 'g', 'd']
print_valid_words(dictionary, char_array)</pre>



<pre id="block-d445ef01-e4eb-4e15-912c-248ff04d63cc" class="wp-block-preformatted output"><strong>Output</strong>
['cat', 'god', 'dog', 'act']</pre>



<hr class="wp-block-separator"/>



<h2>3. Conclusion</h2>



<p>In this Python example, we discussed how to print all valid words that can be formed using characters from a given character array, using a dictionary in Python. We explored two methods to solve this problem</p>



<hr class="wp-block-separator"/>



<p><strong>Helpful Links</strong></p>



<p>Please follow the <strong><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener"></a><a href="https://www.codingeek.com/python-tutorials/" target="_blank" rel="noreferrer noopener">Python tutorial series</a></strong> or the <strong>menu in the sidebar</strong> for the complete tutorial series.</p>



<p>Also for examples in Python and practice please refer to <a href="https://www.codingeek.com/python-examples/" target="_blank" rel="noreferrer noopener"><strong>Python Examples</strong></a>.</p>



<p>Complete code samples are present on <a href="https://github.com/HiteshGarg/codingeek/tree/master/Python" target="_blank" rel="noreferrer noopener">Github project</a>.</p>



<p><strong>Recommended Books</strong></p>



<a href="https://www.amazon.in/dp/0134692888?fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=7b91f6aa9d7b049597656cd5bd5817c5&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=0134692888&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=0134692888" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Automate-Boring-Stuff-Python-2nd/dp/1593279922?pd_rd_w=HfTHM&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=ETWXH8FK0CG38E458SP2&amp;pd_rd_r=2626052a-2870-4319-bab0-3a3aa2223acc&amp;pd_rd_wg=jwnfU&amp;pd_rd_i=1593279922&amp;psc=1&amp;fbclid=IwAR0eDbPtsiCNSquPW3YHeJTJd3m8zHUo6PVY0EQIKNrDXFf2Z5Y_HyCB0tg&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ebcf34b169e248bb339c98b4ea19c821&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279922&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279922" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">

<a style="padding-left:3px" href="https://www.amazon.in/Python-Crash-Course-Eric-Matthes/dp/1593279280?pd_rd_w=PgvgO&amp;pf_rd_p=bc24ecda-5294-487e-a99f-2f95603a59d4&amp;pf_rd_r=Z1YZ0CRKV31V2646SJFC&amp;pd_rd_r=88dac74d-ed99-4d91-801d-094fabe7ae3c&amp;pd_rd_wg=T52yH&amp;pd_rd_i=1593279280&amp;psc=1&amp;fbclid=IwAR31g6aR1FSBvqWwmoBcop-XftKLyG8Ak6jKNGUKvxohTiVQ6Gt8fSKsyUc&amp;linkCode=li3&amp;tag=codingeek-21&amp;linkId=ff7918765640a1302a16c2eed995fb20&amp;language=en_IN&amp;ref_=as_li_ss_il" target="_blank" rel="noopener"><img border="0" src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=1593279280&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=IN&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=codingeek-21&amp;language=en_IN"></a><img loading="lazy" src="https://ir-in.amazon-adsystem.com/e/ir?t=codingeek-21&amp;language=en_IN&amp;l=li3&amp;o=31&amp;a=1593279280" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">



<hr class="wp-block-separator"/>



<p><em><strong>An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding</strong></em></p>



<p><em><strong>Do not forget to share and Subscribe.</strong></em></p>



<p><em><strong>Happy coding!! </strong></em>?</p>



<p></p><p>The post <a href="https://www.codingeek.com/python-examples/print-all-valid-possible-words/">Given a dictionary and a character array, print all valid words that are possible in Python</a> first appeared on <a href="https://www.codingeek.com">Codingeek</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.codingeek.com/python-examples/print-all-valid-possible-words/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
