How to Iterate over a Dictionary in Python

Here are several ways to iterate over a dictionary in Python: (1) Iterate over the keys of a dictionary: for key in my_dict.keys(): print(key) (2) Iterate over the values of a dictionary: for value in my_dict.values(): print(value) (3) Iterate over both the keys and values of a dictionary: for key, value in my_dict.items(): print(key, value) … Read more

Iterate over two Lists in Python simultaneously

You can iterate over two lists (or more) in Python using a zip() function: Here is an example of iterating over two lists (list_a and list_b) at the same time: list_a = [1, 2, 3, 4, 5] list_b = [5, 6, 7, 8, 9] list_c = [] for x, y in zip(list_a, list_b): sum_lists = … Read more

How to Remove the First Rows in Pandas DataFrame

You may use the following syntax to remove the first row/s in Pandas DataFrame: (1) Remove the first row in a DataFrame: df = df.iloc[1:] (2) Remove the first n rows in a DataFrame: df = df.iloc[n:] Next, you’ll see how to apply the above syntax using practical examples. Examples of Removing the First Rows … Read more

How to Flatten a List of Lists in Python

Here are two ways to flatten a list of lists in Python: (1) Using a list comprehension: flatten_list = [i for s in list_of_lists for i in s] (2) Using for loop: flatten_list = [] for s in list_of_lists: for i in s: flatten_list.append(i) Examples Example 1: Flatten a list of lists in Python using … Read more

4 Ways to Remove Empty Strings from a List

Here are 4 ways to remove empty strings from a list in Python: (1) Using a list comprehension: new_list = [x for x in list_with_empty_strings if x != ”] (2) Using for loop: new_list = [] for x in list_with_empty_strings: if x != ”: new_list.append(x) (3) Using filter: new_list = list(filter(None, list_with_empty_strings)) (4) Using filter … Read more

Convert two Lists into a Dictionary in Python

Here are two ways to convert two lists into a dictionary in Python: (1) Using zip() and dict(): list_keys = [‘item_1’, ‘item_2’, ‘item_3’, …] list_values = [‘item_1’, ‘item_2’, ‘item_3’, …] new_dict = dict(zip(list_keys, list_values)) (2) Using a Dictionary Comprehension: list_keys = [‘item_1’, ‘item_2’, ‘item_3’, …] list_values = [‘item_1’, ‘item_2’, ‘item_3’, …] new_dict = {k: v … Read more

How to Create a Dictionary in Python

Here are 2 ways to create a Dictionary in Python: (1) Using curly brackets {} my_dictionary = {1: ‘aaa’, 2: ‘bbb’, 3: ‘ccc’} (2) Using the dict() function my_dictionary = dict({1: ‘aaa’, 2: ‘bbb’, 3: ‘ccc’}) Note that each dictionary stores data in key:value pairs. In addition, the keys must be unique and cannot be … Read more

4 Ways to Extract Dictionary Keys as a List

Here are 4 ways to extract dictionary keys as a list in Python: (1) Using a list() function: my_list = list(my_dict) (2) Using dict.keys(): my_list = list(my_dict.keys()) (3) Using List Comprehension: my_list = [i for i in my_dict] (4) Using For Loop: my_list = [] for i in my_dict: my_list.append(i) Examples of extracting dictionary keys … Read more

Convert Python List to a NumPy Array

The following syntax can be used to convert a Python list to a numpy array: my_array = np.array(my_list) In this guide, you’ll see how to convert: Python list to a numpy array List of lists (multi-dimensional list) to a numpy array (1) Convert Python List to a NumPy Array Let’s create a simple list with … Read more

How to Convert NumPy Array to a List in Python

You can use tolist() in order to convert a numpy array to a list in Python: my_list = my_array.tolist() Below are the steps to convert a numpy array to a list (as well as to a list of lists) using practical examples. Steps to Convert NumPy Array to a List in Python Step 1: Create … Read more