Sort a Dictionary in Python by Keys

To sort a dictionary in Python by keys: 1. In an ascending order: Copy my_dict = {4: “d”, 2: “b”, 5: “e”, 1: “a”, 3: “c”}sorted_dict = dict(sorted(my_dict.items()))print(sorted_dict) Output: 2. In a descending order: Copy my_dict = {4: “d”, 2: “b”, 5: “e”, 1: “a”, 3: “c”}sorted_dict = dict(sorted(my_dict.items(), reverse=True))print(sorted_dict) Output: Function to sort a … Read more

How to Sort a List in Python

Here are 2 ways to sort a list in Python: 1. Using the ‘sorted()’ function (returning a new sorted list): To sort a list in an ascending order: Copy my_list = [“D”, “X”, “A”, “Z”, “E”]sorted_list = sorted(my_list)print(sorted_list) The result: To sort a list in a descending order: Copy my_list = [“D”, “X”, “A”, “Z”, … Read more

Remove Duplicates from a Python List

Here are 2 ways to remove duplicates from a Python list: 1. Using set (order is not preserved): Copy my_list = [“aa”, “aa”, “dd”, “xyz”, “aa”, “pp”, “pp”]unique_list = list(set(my_list))print(unique_list) The result: 2. Using dict.fromkeys() where the order is preserved: Copy my_list = [“aa”, “aa”, “dd”, “xyz”, “aa”, “pp”, “pp”]unique_list = list(dict.fromkeys(my_list))print(unique_list) The result: Create … Read more

How to Count Duplicates in a Python List

Here are 2 ways to count duplicates in a Python List: 1. Using the “count” method: Copy my_list = [“a”, “a”, “z”, “z”, “s”, “a”, “a”, “b”, “b”, “k”]my_dict = {i: my_list.count(i) for i in my_list}print(my_dict) The result: 2. Using collections: Copy import collectionsmy_list = [“a”, “a”, “z”, “z”, “s”, “a”, “a”, “b”, “b”, “k”]my_dict … Read more

How to use reversed() in Python

The ‘reversed()‘ function in Python can be used to reverse an iterable (list, tuple, string, etc.): 1. Reverse a list: Copy my_list = [“a”, “b”, “c”, “d”, “e”]reversed_list = list(reversed(my_list))print(reversed_list) The result: 2. Reverse a tuple: Copy my_tuple = (“a”, “b”, “c”, “d”, “e”)reversed_tuple = tuple(reversed(my_tuple))print(reversed_tuple) The result: 3. Reverse a string: Copy my_string = … Read more

How to use pop() in Python

The ‘pop()‘ method in Python can be used to remove (and also return) an item from a list based on a specific index. If no index is provided, the default behavior is to remove (and return) the last item from the list: 1. Remove and return the last item from a list (default behavior): Copy … Read more

How to use join() in Python

The ‘join()‘ method in Python can be used to concatenate items of an iterable (such as a list, tuple or string) into a single string: 1. To join the items of a list of strings into a single string: Copy my_list = [“apple”, “grape”, “mango”, “peach”, “lemon”]my_string = “, “.join(my_list)print(my_string)print(type(my_string)) The result: 2. To join … Read more

How to use split() in Python

The ‘split()‘ method in Python can be used to split a string into a list of substrings based on a delimiter. 1. Split a string into a list of words (where the default delimiter is whitespace): Copy my_string = “Hello world, enjoy the ride!”my_list = my_string.split()print(my_list) The result: 2. Split a string based on a … Read more

How to use the range() function in Python

The ‘range()‘ function can be used to generate a sequence of numbers: 1. Generate a simple sequence of numbers: Copy for i in range(5): print(i) The result: 2. Specify a start value (e.g., 2) and a stop value (e.g., 6): Copy for i in range(2, 6): print(i) The result: 3. Use a step value (e.g., … Read more

How to use enumerate() in Python

You can use ‘enumerate()‘ in Python to loop over an iterable (such as a list, tuple, or string) while also keeping track of the index of the current item. 1. Use ‘enumerate()‘ with a list: Copy my_list = [“apple”, “grape”, “mango”, “peach”, “lemon”]for idx, item in enumerate(my_list): print(idx, item) The result: To specify the start … Read more