Python Dictionary

How to Find the Maximum Value in a Python Dict?

There are three problem variants of finding the maximum value in a Python dictionary: Find the maximum value and return this maximum value Find the maximum value and return a (key, value) tuple of both the key and the max value itself Find the maximum value and return only the key assigned to the max …

How to Find the Maximum Value in a Python Dict? Read More »

How to Sort a Dictionary in Python By Key?

To sort a dictionary by key in Python, use the dictionary comprehension expression {key:dict[key] for key in sorted(dict)} to create a new dictionary with keys in sorted order. You iterate over all keys in sorted order, obtain their associated values with rank[key], and put them into a new dictionary. Python dictionaries preserve the order—even if …

How to Sort a Dictionary in Python By Key? Read More »

Dictionaries and Unpacking Arguments in Python

Programming is about using lower-level functionality to create higher-level functionality. In general, any programming language is a collection of functions that in turn build upon functions provided by the operating system. You must master the art of building your own code with the help of existing functionality, instead of reinventing the wheel! Keyword Arguments Functions …

Dictionaries and Unpacking Arguments in Python Read More »

Python dir() — A Simple Guide with Video

If used without argument, Python’s built-in dir() function returns the function and variable names defined in the local scope—the namespace of your current module. If used with an object argument, dir(object) returns a list of attribute and method names defined in the object’s scope. Thus, dir() returns all names in a given scope. Usage Learn …

Python dir() — A Simple Guide with Video Read More »

Python dict() — A Simple Guide with Video

Python’s built-in dict() function creates and returns a new dictionary object from the comma-separated argument list of key = value mappings. For example, dict(name = ‘Alice’, age = 22, profession = ‘programmer’) creates a dictionary with three mappings: {‘name’: ‘Alice’, ‘age’: 22, ‘profession’: ‘programmer’}. A dictionary is an unordered and mutable data structure, so it …

Python dict() — A Simple Guide with Video Read More »

How To Sort A Dictionary By Value in Python?

Summary: Use one of the following methods to sort a dictionary by value: Using The sorted(dict1, key=dict1.get) Method. Using Dictionary Comprehension And Lambda With sorted() Method. Using OrderedDict (For Older Versions Of Python). Using itemgetter() with sorted() Method. Using Counter subclass. Problem: Given a dictionary; how to sort it by values? Example: The following example …

How To Sort A Dictionary By Value in Python? Read More »

Python Double Asterisk (**)

Summary: The double asterisk operator has the following uses: a**b – Exponentiation. def f(**kwargs) – Unpacking: Defining an arbitrary number of keyword arguments. f(**d) – Dictionary Unpacking. f(**d1,**d2) – Merging Dictionaries. While using a function in your program, you might be uncertain about the number of named arguments that have to be passed into the …

Python Double Asterisk (**) Read More »

How To Pass a Variable By Reference In Python?

Summary: Variables are passed by object reference in Python. Therefore, mutable objects are passed by reference while immutable objects are passed by value in Python. To pass immutable objects by reference you can return and reassign the value to the variable, or pass it inside a dictionary/list, or create a class and use the self …

How To Pass a Variable By Reference In Python? Read More »

How to Remove Duplicates From a Python List While Preserving Order?

To remove duplicates from a Python list while preserving the order of the elements, use the code list(dict.fromkeys(list)) that goes through two phases: (1) Convert the list to a dict using the dict.fromkeys() function with the list elements as keys and None as dict values. (2) Convert the dictionary back to a list using the …

How to Remove Duplicates From a Python List While Preserving Order? Read More »

How To Update A Key In A Dictionary In Python If The Key Doesn’t Exist?

Summary: To update a key in a dictionary if it doesn’t exist, you can check if it is present in the dictionary using the in keyword along with the if statement and then update the key-value pair using subscript notation or update() method or the * operator. Another workaround for this is, using the setdefault(key[,¬†default]) …

How To Update A Key In A Dictionary In Python If The Key Doesn’t Exist? Read More »

How To Format A String That Contains Curly Braces In Python?

Summary: Use one of the following methods to format strings that contain curly braces: Use double curly braces {{}} Use the old string formatting, i.e. the % operator Use the JSON Library Use Template Strings Problem: Given a string literal with curly braces; how to format the string and ensure that the curly braces are …

How To Format A String That Contains Curly Braces In Python? Read More »

56 Python One-Liners to Impress Your Friends

This is a running document in which I’ll answer all questions regarding the single line of Python code. It’s based on my interactive collection here but without the slow videos and embedded code shells. Let’s get started! Python One Line If Else You can use a simple if statement in a single line of code. …

56 Python One-Liners to Impress Your Friends Read More »