Iterate over a List of Lists in Python

The following syntax can be used to iterate over a list of lists: Copy my_list = [[“a”, “b”, “c”], [“d”, “e”, “f”], [“g”, “h”, “i”]]for x in my_list: for y in x: print(y) Example of iterating over a list of lists Let’s suppose that you have a list of lists of colors. You can then … Read more

How to Iterate over a Dictionary in Python

Here are 3 ways to iterate over a dictionary in Python: (1) Iterate over the keys of a dictionary: Copy for key in my_dict.keys(): print(key) (2) Iterate over the values of a dictionary: Copy for value in my_dict.values(): print(value) (3) Iterate over both the keys and values of a dictionary: Copy for key, value in … 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: Copy 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 = x + … Read more

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: Copy df = df.iloc[1:] (2) Remove the first n rows in a DataFrame: Copy df = df.iloc[n:] Examples of Removing the First Rows in a DataFrame Example 1: Remove the first row in … Read more

Flatten a List of Lists in Python

Here are two ways to flatten a list of lists in Python: (1) Using a List Comprehension: Copy flatten_list = [i for s in list_of_lists for i in s] (2) Using a For Loop: Copy 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 … Read more

Remove Empty Strings from Python List

Here are 4 ways to remove empty strings from a list in Python: (1) Using a List Comprehension: Copy new_list = [x for x in list_with_empty_strings if x != “”] (2) Using a For Loop: Copy new_list = []for x in list_with_empty_strings: if x != “”: new_list.append(x) (3) Using Filter: Copy new_list = list(filter(None, list_with_empty_strings)) … 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(): Copy 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: Copy list_keys = [“item_1”, “item_2”, “item_3”, …]list_values = [“item_1”, “item_2”, “item_3”, …]new_dict = {k: v for k, … Read more

How to Create a Dictionary in Python

Here are 2 ways to create a Dictionary in Python: (1) Using curly brackets {} Copy my_dictionary = {1: “aaa”, 2: “bbb”, 3: “ccc”} (2) Using the dict() function Copy 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 … 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: Copy my_list = list(my_dict) (2) Using dict.keys(): Copy my_list = list(my_dict.keys()) (3) Using List Comprehension: Copy my_list = [i for i in my_dict] (4) Using For Loop: Copy my_list = [] for i in my_dict: my_list.append(i) Examples … Read more

Convert Python List to NumPy Array

To convert a Python list to a NumPy array: Copy my_array = np.array(my_list) In this guide, you’ll see how to convert: Case 1: Convert Python List to a NumPy Array To start, create a simple list with 6 elements: Copy my_list = [10, 15, 20, 25, 30, 35]print(my_list)print(type(my_list)) This is how the list would look … Read more