What is the result of the below Python code: from collections import Counterinput_dictionary = {‘a’:5, ‘b’:3, ‘c’: 1, ‘b’:2}print(Counter(input_dictionary)) Result:Counter({‘a’: 5, ‘b’: 2, ‘c’: 1}) Question: Even-though we have 2 values for the key ‘b’ in “input_dictionary”, the result has only one ‘b’ key/value pair. Do you know why? What’s the reason?
Category Archives: Python
Python Random Tips #5
This is not a Python Starter Tutorial but advanced Python Tips in a Random Order. What is Python Counter? Python Counter is a container that keeps a count of the number of occurrences of any value in the container. Python Counter helps to count the key-value pairs in an object. With the inputs such asContinue reading “Python Random Tips #5”
Python Random Tips #4
This is not a Python Starter Tutorial but advanced Python Tips in a Random Order. What are the differences between sorted() and sort() : #1 The output of the sorted() built-in function is a new list and no changes to the original input. As you can see the original ‘colors’ list is unchanged after theContinue reading “Python Random Tips #4”
Python Random Tips #3
This is not a Python Starter Tutorial but advanced Python Tips in a Random Order. What is Lambda and why we use them? ‘def’ is used to build functions in Python. ‘Lambda’ is also another tool that can be used to build functions in Python. Lambda is referred as a tool to create anonymous function.Continue reading “Python Random Tips #3”
Python Random Tips #2
This is not a Python Starter Tutorial but advanced Python Tips in a Random Order. What is the use of enumerate? Look at this example: In this example, we have a list with 4 items (language names). We can list the index and the list item using the for loop and also enumerate function. SinceContinue reading “Python Random Tips #2”
Python Random Tips #1
This is not a Python Starter Tutorial but advanced Python Tips in a Random Order. We have the following 3 Python lists. How to extract and displays the list contents? Option 1:Here is one option to display the content of all the three lists.Using the index numbers, the list items are displayed below. Here isContinue reading “Python Random Tips #1”