avatarHaider Imtiaz

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

10404

Abstract

eyword">print</span>(filtered) <span class="hljs-meta"># 3421</span></pre></div><h2 id="bae0">9. Make First Letter Capital</h2><p id="98f2">This is another daily problem-solving snippet you can make your String every word first Letter Capital. You had just used the <b>title()</b> method with your target string.</p><div id="4292"><pre><span class="hljs-comment"># Capital the First letter</span></pre></div><div id="03e4"><pre><span class="hljs-built_in">string</span> = <span class="hljs-string">"hy, i'm a codder"</span> <span class="hljs-built_in">print</span>(<span class="hljs-built_in">string</span>.<span class="hljs-built_in">title</span>()) # Hy, I'M A Codder.</pre></div><div id="4fd6"><pre>string2 = <span class="hljs-string">"learn programming language"</span> <span class="hljs-keyword">print</span>(string2.title()) <span class="hljs-meta"># Learn Programming Language</span></pre></div><h2 id="188c">10. Hex to RGB</h2><p id="9b4d">These are the most useful snippets for every Front end developer or designer. You can convert Hexadecimal Color Codes to RGB Code which is a format like that (Red, Green, Blue ). Check out the below code example to know how to do that.</p><div id="fdc0"><pre>#Hex <span class="hljs-keyword">to</span> RGB</pre></div><div id="74ba"><pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">Hex_To_Rgb</span>(<span class="hljs-params"><span class="hljs-built_in">hex</span></span>): rgb = <span class="hljs-built_in">tuple</span>(<span class="hljs-built_in">int</span>(<span class="hljs-built_in">hex</span>[x: x+<span class="hljs-number">2</span>], <span class="hljs-number">16</span>) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> (<span class="hljs-number">0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>)) <span class="hljs-keyword">return</span> rgb</pre></div><div id="1ee8"><pre><span class="hljs-attribute">print</span>(Hex_To_Rgb('FF5733')) # (<span class="hljs-number">255</span>, <span class="hljs-number">87</span>, <span class="hljs-number">51</span>) <span class="hljs-attribute">print</span>(Hex_To_Rgb('<span class="hljs-number">33</span>D8FF')) # (<span class="hljs-number">51</span>, <span class="hljs-number">216</span>, <span class="hljs-number">255</span>)</pre></div><h2 id="67c5">11. Sort Dictionary</h2><p id="3634">We had seen snippets to sort a list Okay Cool. W<b>hat about sorting Dictionaries ?</b>. Dictionary is sort by either Key or value. We will see diction sort example code by using the same built-in sorting method with little modification.</p><div id="7bd7"><pre><span class="hljs-meta"># Sorting Dictionary</span></pre></div><div id="fa80"><pre><span class="hljs-comment">#method 1 </span> <span class="hljs-attribute">mydict</span> = {<span class="hljs-number">3</span>:<span class="hljs-string">"c"</span>, <span class="hljs-number">2</span>:<span class="hljs-string">"b"</span>, <span class="hljs-number">1</span>:<span class="hljs-string">"a"</span>} <span class="hljs-attribute">print</span>(dict(sorted(mydict.items(), key=lambda x: x[<span class="hljs-number">1</span>]))) # {<span class="hljs-number">1</span>: 'a', <span class="hljs-number">2</span>: 'b', <span class="hljs-number">3</span>: 'c'}</pre></div><div id="5443"><pre>#<span class="hljs-keyword">method</span> 2 <span class="hljs-title function_">mydict2</span> = [ <span class="hljs-comment">{ "Name": "Haider", "Age": 22, "CGPA": 3.45 }</span>,</pre></div><div id="4eba"><pre><span class="hljs-punctuation">{</span> <span class="hljs-attr">"Name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Charles"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"Age"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">30</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"CGPA"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">3.22</span> <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span></pre></div><div id="6276"><pre><span class="hljs-punctuation">{</span> <span class="hljs-attr">"Name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Isabella"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"Age"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">21</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"CGPA"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">3.12</span> <span class="hljs-punctuation">}</span> <span class="hljs-punctuation">]</span></pre></div><div id="e5ea"><pre><span class="hljs-built_in">print</span>(sorted(mydict2, <span class="hljs-attribute">key</span>=lambda item: item.<span class="hljs-built_in">get</span>(<span class="hljs-string">"Name"</span>)))</pre></div><div id="71dc"><pre><span class="hljs-meta">#Output </span> <span class="hljs-meta"># [{<span class="hljs-string">'Name'</span>: <span class="hljs-string">'Charles'</span>, <span class="hljs-string">'Age'</span>: 30, <span class="hljs-string">'CGPA'</span>: 3.22}, {<span class="hljs-string">'Name'</span>: <span class="hljs-string">'Haider'</span>, <span class="hljs-string">'Age'</span>: 22, <span class="hljs-string">'CGPA'</span>: 3.45}, {<span class="hljs-string">'Name'</span>: <span class="hljs-string">'Isabella'</span>, <span class="hljs-string">'Age'</span>: 21, <span class="hljs-string">'CGPA'</span>: 3.12}]</span></pre></div><h2 id="e4d4">12. Lower Case String</h2><p id="5b78">This snippet will show you how to make the lower case your string in a second using Python built-in method.</p><div id="5141"><pre>#Lower <span class="hljs-keyword">Case</span> <span class="hljs-built_in">String</span></pre></div><div id="e48e"><pre><span class="hljs-attribute">Str1</span> <span class="hljs-operator">=</span> <span class="hljs-string">"Python is a Programming Language"</span> <span class="hljs-attribute">Str2</span> <span class="hljs-operator">=</span> <span class="hljs-string">"PYTHON SNIPPETS FOR DAILY LIFE PROBLEM"</span></pre></div><div id="75cb"><pre><span class="hljs-comment">#converting to lower case </span> <span class="hljs-built_in">print</span>(Str1.lower()) <span class="hljs-comment"># python is a programming language</span> <span class="hljs-built_in">print</span>(Str2.lower()) <span class="hljs-comment"># python snippets for daily life problem</span></pre></div><h2 id="d15a">13. Check List is Empty or Not</h2><p id="0b5b">This is another Problem we had a face when we receive data in the list from a function or HTTP server or anywhere and want to check whether or list is empty or not. The Below code example will show you the two ways to check the list is empty or not.</p><div id="7bf6"><pre><span class="hljs-comment">#checking list is empty or not</span></pre></div><div id="907e"><pre>mylst = list() # <span class="hljs-keyword">empty</span> list #<span class="hljs-keyword">method</span> 1 <span class="hljs-title function_">if</span> <span class="hljs-title function_">len</span><span class="hljs-params">(mylst)</span> == 0: pass</pre></div><div id="29ee"><pre>#<span class="hljs-keyword">method</span> 2 <span class="hljs-title function_">if</span> <span class="hljs-title function_">not</span> <span class="hljs-title function_">mylst</span>: pass</pre></div><h2 id="69d0">14. Cloning a List</h2><p id="4d82">This snippet will show how to copy a list to another list using the simple copy method and deep copy method. Now you no longer need to used Loops for copying a list check out the below example code.</p><div id="697a"><pre><span class="hljs-meta"># List Cloning</span> <span class="hljs-keyword">import</span> copy</pre></div><div id="3c26"><pre><span class="hljs-attr">mylist</span> = [<span class="hljs-number">5</span>, <span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">99</span>, <span class="hljs-number">100</span>, <span class="hljs-number">123</span>, <span class="hljs-number">434</span>, <span class="hljs-number">566</span>]</pre></div><div id="fa72"><pre>#<span class="hljs-keyword">method</span> 1</pre></div><div id="497b"><pre>clone_list1 = <span class="hljs-keyword">copy</span>.<span class="hljs-keyword">copy</span>(mylist)</pre></div><div id="746d"><pre>clo<span class="hljs-symbol">ne_list2</span> = copy.deepcopy<span class="hljs-comment">(mylist)</span></pre></div><div id="132b"><pre><span class="hljs-built_in">print</span>(clone_list1) # <span class="hljs-selector-attr">[5, 4, 2, 99, 100, 123, 434, 566]</span> <span class="hljs-built_in">print</span>(clone_list2) # <span class="hljs-selector-attr">[5, 4, 2, 99, 100, 123, 434, 566]</span></pre></div><div id="7575"><pre>#<span class="hljs-keyword">method</span> 2</pre></div><div id="46db"><pre><span class="hljs-keyword">clone_list3 </span>= [x for x in mylist] print(<span class="hljs-keyword">clone_list3) </span><span class="hljs-comment"># [5, 4, 2, 99, 100, 123, 434, 566]</span></pre></div><h2 id="d9f9">15. Two List To Dictionary</h2><p id="c242">This snippet code you understand by its name we will convert the two list into a dictionary using the <b>zip()</b> method. Zip method first argument will be a key and the second will be valued.</p><div id="ba25"><pre><span class="hljs-comment"># Two List to Dictionary</span> <span class="hljs-attr">keys</span> = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>] <span class="hljs-attr">values</span> = [<span class="hljs-string">"Python"</span>, <span class="hljs-string">"JavaScript"</span>, <span class="hljs-string">"Dart"</span>]</pre></div><div id="5c3e"><pre><span class="hljs-attr">dicti</span> = dict(zip(keys,values))</pre></div><div id="5c0a"><pre><span class="hljs-built_in">print</span>(dicti) # {1: <span class="hljs-string">'Python'</span>, 2: <span class="hljs-string">'JavaScript'</span>, 3: <span class="hljs-string">'Dart'</span>}</pre></div><h2 id="ec33">Final Thoughts</h2><p id="22cc">I hope you had enjoyed these snippet codes and use them in your project. Make a bookmark for this article for future use. These are common snippets that we need for daily life Python Problems. Feel Free to share your response and snippets with the community.<b> Happ

Options

y Python Coding!</b></p><h2 id="292d">What Next?</h2><p id="bd96">Never stop learning, check out my other Programming related articles, I hope you will enjoy them also.</p><div id="b011" class="link-block"> <a href="https://python.plainenglish.io/22-useful-snippets-to-code-like-a-pro-in-python-1d0dcaacac69"> <div> <div> <h2>22 Useful Snippets to Code like a Pro in Python</h2> <div><h3>Get vowels, finding anagrams, sorting dictionary, n times string, byte sizing, filtering, etc. You will learn much more…</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*8lhZBBja11Pf4_K1IYCj-Q.png)"></div> </div> </div> </a> </div><div id="e7a5" class="link-block"> <a href="https://python.plainenglish.io/top-python-tricks-that-will-boost-your-skills-c55e8268ca5f"> <div> <div> <h2>Top Python Tricks That Will Boost Your Skills</h2> <div><h3>Most used tricks that will boost your Python programming skills.</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*3Fr1uaEJfdad-mVLbPMaIg.png)"></div> </div> </div> </a> </div><div id="41da" class="link-block"> <a href="https://python.plainenglish.io/hidden-features-of-python-that-you-have-probably-never-heard-of-47af1e8abee2"> <div> <div> <h2>Python Hidden Features That You Probably Never Heard Of</h2> <div><h3>Lesser-known Python Features, Tips, and Tricks</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*NKDm9kgiBXLl2SNElFmRBg.jpeg)"></div> </div> </div> </a> </div><div id="e2e6" class="link-block"> <a href="https://python.plainenglish.io/22-helpful-python-tips-and-tricks-for-beginners-43c4a8f56a1c"> <div> <div> <h2>Useful Tips and Tricks for Python Programmers</h2> <div><h3>Super Awesome Tips and Tricks for better and Pro coding in Python you should know as a Programmer.</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*Ro7Fogu5keiL5jA4.png)"></div> </div> </div> </a> </div><div id="ed86" class="link-block"> <a href="https://python.plainenglish.io/17-python-tips-and-tricks-to-boost-your-coding-skills-5e14054001b2"> <div> <div> <h2>17 Python Tips and Tricks to Boost your Coding Skills</h2> <div><h3>Make your life easier with these Top Python Tips and Tricks</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*Q5zOL3rCGnljaBnrwrpr0w.png)"></div> </div> </div> </a> </div><div id="8c43" class="link-block"> <a href="https://python.plainenglish.io/from-zero-to-hero-in-python-in-just-10-minutes-83064ffd5aee"> <div> <div> <h2>From Zero to Hero in Python in Just 10 minutes</h2> <div><h3>Everything you need to learn to go from zero to hero in Python.</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*uogTxM0a-lVa__VkjFBjew.jpeg)"></div> </div> </div> </a> </div><div id="e87e" class="link-block"> <a href="https://python.plainenglish.io/25-programming-jokes-to-make-you-lol-8bf793eb2a52"> <div> <div> <h2>25 Programming Jokes To Make You LOL</h2> <div><h3>Some funny programming jokes to make you smile and laugh</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*x3fGnT5-vln_l_OK)"></div> </div> </div> </a> </div><div id="cd00" class="link-block"> <a href="https://python.plainenglish.io/top-python-shortcuts-c0345d5a48ec"> <div> <div> <h2>Top Python Shortcuts</h2> <div><h3>15 Python Tips and Tricks that make your life easier</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*-IYcE6-YJTX_brw67C6aRA.jpeg)"></div> </div> </div> </a> </div><div id="3ed4" class="link-block"> <a href="https://python.plainenglish.io/12-python-interesting-facts-that-you-might-not-know-72dcb90b8ea"> <div> <div> <h2>12 Python Interesting Facts That You Might Not Know</h2> <div><h3>12 Facts I Bet You Didn’t Know About Python</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*X_xqpHrzODjl8K4YMOGAuQ.jpeg)"></div> </div> </div> </a> </div><div id="80d0" class="link-block"> <a href="https://python.plainenglish.io/python-snippets-you-should-learn-today-e796456bd6db"> <div> <div> <h2>Python Snippets You Should Learn Today</h2> <div><h3>Top Python snippets that save your valuable time and make your life easier.</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*fqvnEW_Ixk3vfLQu44xjRA.jpeg)"></div> </div> </div> </a> </div><div id="0264" class="link-block"> <a href="https://python.plainenglish.io/best-ways-to-earn-1000-in-a-month-with-programming-fbb70cf553da"> <div> <div> <h2>7 Ways to Earn 1000 in a Month with Programming</h2> <div><h3>In this article, we will see the best ways to earn 1000 online in a month if you are a programmer. Well, you had…</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*4rfHyoDrhAlptiYq)"></div> </div> </div> </a> </div><div id="3762" class="link-block"> <a href="https://python.plainenglish.io/build-your-first-simple-django-web-app-with-python-9a4452157089"> <div> <div> <h2>Build your first Simple Django Web App With Python</h2> <div><h3>Django is a Python open-source web development framework for rapid web app design. Today, we’ll introduce Django and…</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*khNdKoKBXaIczaax)"></div> </div> </div> </a> </div><div id="8a8c" class="link-block"> <a href="https://python.plainenglish.io/step-by-step-guide-to-converting-your-py-file-to-an-exe-file-f86eb0b5f253"> <div> <div> <h2>Step by Step Guide to Converting Your .py File to an .exe File</h2> <div><h3>Looking for a guide for converting your python file to an executable ( .exe ) file. In this article, we will learn…</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*GvPP9cBdy2M7VqYgge9SQQ.png)"></div> </div> </div> </a> </div><div id="9eac" class="link-block"> <a href="https://python.plainenglish.io/how-to-read-and-write-pdf-files-using-python-7b930977fe58"> <div> <div> <h2>How to Read and Write PDF files using Python</h2> <div><h3>Extract Text, Tables, Images from PDF Files, and much more to learn in this article</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*x56MaZgeveeGzOZSGoVK6Q.jpeg)"></div> </div> </div> </a> </div><div id="b47a" class="link-block"> <a href="https://python.plainenglish.io/how-to-read-and-write-to-json-file-in-python-ef35460aaeb5"> <div> <div> <h2>How to Read and Write to JSON File in Python</h2> <div><h3>JSON stands for JavaScript Object Notation. JSON is a lightweight format for storing and transferring data, even it is…</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*7egri62bf4B8ebEmK17RfA.jpeg)"></div> </div> </div> </a> </div><p id="7441"><i>More content at <a href="http://plainenglish.io"><b>plainenglish.io</b></a></i></p></article></body>

15 Useful Snippets for your Daily life Python Problems

Digitizing, Generic Swapping, Hex_To_RGB, Print Same line Sort Dictionary, and many more useful snippets

Photo by Roman Synkevych on Unsplash

Python is my most Favorite language and finds snippets for daily life Python problem is my hobby. I will brief 15 Useful Snippets for your Daily Life Python Problems. These snippets will help you in the daily Programming Problems that you face and write long code for them.

You will be familiar with most of the snippets but many of them will be new for you. These snippets are easy to learn and you freely use them in your code. Make a bookmark to this article for future use and let get started without wasting any further time.

1. Generic Swapping

I can bet you used a temp variable for swapping in many programming languages. But Python allows you to swap two values without any temp variable. Check the below code for example.

# generic swapping
x = 10
y = 12
x , y = y , x
print(x, y) # 12 10

2. Digitize into List

Digitizing means converting a number into an array or list. This comes in handy when you need to convert a large number into a list form.

Below code example use map() built-in method taking two arguments. In last we had converted the output result from the map method into the list.

# Digitize
def Digitizing(num):
    return list(map(int, str(num)))
num1 = 4858
num2 = 7804
print(Digitizing(num1))  # [4, 8, 5, 8]
print(Digitizing(num2))  # [7, 8, 0, 4]

3. Easy Sorting

Sorting is a Part of everyday problems. We had to sort a list of numbers, names, and massive amounts of data and etc. Python had some good sorting methods that will save your time for writing manually a sorting function.

Below is the sorting example code in which I sort the list with two different sorting functions.

# Sorting with Built-in
# method 1
List = [500, 300, 200, 100]
print(sorted(List)) # [100, 200, 300, 500]
#method 2
List = [100, 500, 300, 400]
List.sort()
print(List) # [100, 300, 400, 500]

4. Way to Print on the Same Line

Another Solution for your everyday problem is Printing/Output data on the same line. We all Know that the Print() statement outputs the data on console line by line. What about if we are required to output on the same line?

Let suppose you making a console game in Python and you want to output a Print statement on the same line. Check out the below code example to get a solution for this.

# Good way to write on same line
import sys
sys.stdout.write("Hello world ")
sys.stdout.write("Python is a Programming ")
sys.stdout.write("Langauge ")
# Output
# Hello world Python is a Programming Langauge

5. Printing String N Times

This snippet will save you from loops for printing string “N” times. In Python, you can multiple a string with an integer number and generate a sequence of it.

# Print N TIMES String
# old method
for x in range(5):
    print("👋")
#👋👋👋👋👋
# good method
print("👋" * 5) # 👋👋👋👋👋

6. Find Anagrams

Anagrams are the word or phrase which are made by arranging them. Let say you are doing a text analysis in Python. You will need an Anagrams checking function. So why wasting time writing them. Just check out the following code example and use it for your daily anagram problem.

# Anagram checking
from collections import Counter
def isAnagrams(string1, string2):
    return Counter(string1) == Counter(string2)
print(isAnagrams("bc123", "445bc")) # False
print(isAnagrams("123abc","abc123")) # True

7. Time of Execution

Every Programmer wants its Python program should be generic and had well execution performance. Below snippets will help you know the Time of Execution your program takes. We will use Time built-in Library in Python, Check out the example code below.

# Execuetion Time Calculating
import time
st = time.time()
def fun():
    for x in range(100000):
        print(x)
fun()
et = time.time()
print("Execution take: ", et-st) # 7.870 sec

8. Filter Unique Values

Let Suppose You had junk data and you need to find unique values or remove the duplicates. The first approach you will use is iterating each element and store them in the list and check that list with every element is it appear or not and etc. But these snippets will solve your problem in a second.

# Filtering Unique
data= "111122233334444"
ndata = set(data)
filtered = "".join(ndata)
print(filtered) # 3421

9. Make First Letter Capital

This is another daily problem-solving snippet you can make your String every word first Letter Capital. You had just used the title() method with your target string.

# Capital the First letter
string = "hy, i'm a codder"
print(string.title()) # Hy, I'M A Codder.
string2 = "learn programming language"
print(string2.title()) # Learn Programming Language

10. Hex to RGB

These are the most useful snippets for every Front end developer or designer. You can convert Hexadecimal Color Codes to RGB Code which is a format like that (Red, Green, Blue ). Check out the below code example to know how to do that.

#Hex to RGB
def Hex_To_Rgb(hex):
  rgb = tuple(int(hex[x: x+2], 16) for x in (0, 2, 4))
  return rgb
print(Hex_To_Rgb('FF5733')) # (255, 87, 51)
print(Hex_To_Rgb('33D8FF')) # (51, 216, 255)

11. Sort Dictionary

We had seen snippets to sort a list Okay Cool. What about sorting Dictionaries ?. Dictionary is sort by either Key or value. We will see diction sort example code by using the same built-in sorting method with little modification.

# Sorting Dictionary
#method 1 
mydict = {3:"c", 2:"b", 1:"a"}
print(dict(sorted(mydict.items(), key=lambda x: x[1]))) # {1: 'a', 2: 'b', 3: 'c'}
#method 2
mydict2 = [
 {
    "Name": "Haider",
    "Age": 22,
    "CGPA": 3.45
  },
{
     "Name": "Charles",
     "Age": 30,
     "CGPA": 3.22
  },
{
    "Name": "Isabella",
    "Age": 21,
    "CGPA": 3.12
  }
]
print(sorted(mydict2, key=lambda item: item.get("Name")))
#Output 
# [{'Name': 'Charles', 'Age': 30, 'CGPA': 3.22}, {'Name': 'Haider', 'Age': 22, 'CGPA': 3.45}, {'Name': 'Isabella', 'Age': 21, 'CGPA': 3.12}]

12. Lower Case String

This snippet will show you how to make the lower case your string in a second using Python built-in method.

#Lower Case String
Str1 = "Python is a Programming Language"
Str2 = "PYTHON SNIPPETS FOR DAILY LIFE PROBLEM"
#converting to lower case 
print(Str1.lower()) # python is a programming language
print(Str2.lower()) # python snippets for daily life problem

13. Check List is Empty or Not

This is another Problem we had a face when we receive data in the list from a function or HTTP server or anywhere and want to check whether or list is empty or not. The Below code example will show you the two ways to check the list is empty or not.

#checking list is empty or not
mylst = list() # empty list
#method 1
if len(mylst) == 0:
    pass
#method 2
if not mylst:
    pass

14. Cloning a List

This snippet will show how to copy a list to another list using the simple copy method and deep copy method. Now you no longer need to used Loops for copying a list check out the below example code.

# List Cloning
import copy
mylist = [5, 4, 2, 99, 100, 123, 434, 566]
#method 1
clone_list1 = copy.copy(mylist)
clone_list2 = copy.deepcopy(mylist)
print(clone_list1) # [5, 4, 2, 99, 100, 123, 434, 566]
print(clone_list2) # [5, 4, 2, 99, 100, 123, 434, 566]
#method 2
clone_list3 = [x for x in mylist]
print(clone_list3) # [5, 4, 2, 99, 100, 123, 434, 566]

15. Two List To Dictionary

This snippet code you understand by its name we will convert the two list into a dictionary using the zip() method. Zip method first argument will be a key and the second will be valued.

# Two List to Dictionary
keys = [1, 2, 3]
values = ["Python", "JavaScript", "Dart"]
dicti = dict(zip(keys,values))
print(dicti) # {1: 'Python', 2: 'JavaScript', 3: 'Dart'}

Final Thoughts

I hope you had enjoyed these snippet codes and use them in your project. Make a bookmark for this article for future use. These are common snippets that we need for daily life Python Problems. Feel Free to share your response and snippets with the community. Happy Python Coding!

What Next?

Never stop learning, check out my other Programming related articles, I hope you will enjoy them also.

More content at plainenglish.io

Python
Programming
Coding
Software Development
Python Programming
Recommended from ReadMedium