Convert a List to a Tuple in Python

Here are 3 ways to convert a list to a tuple in Python: (1) Using the tuple() function: Copy my_list = [“item_1”, “item_2”, “item_3”, …]my_tuple = tuple(my_list) (2) Using tuple(i for i in my_list): Copy my_list = [“item_1”, “item_2”, “item_3”, …]my_tuple = tuple(i for i in my_list) (3) Using (*my_list, ): Copy my_list = [“item_1”, … Read more

Convert Tuple to List in Python

Here are 3 ways to convert a tuple to a list in Python: (1) Using a list() function: Copy my_tuple = (“item_1”, “item_2”, “item_3”, …)my_list = list(my_tuple) (2) Using a list comprehension: Copy my_tuple = (“item_1”, “item_2”, “item_3”, …)my_list = [i for i in my_tuple] (3) Using a for loop: Copy my_tuple = (“item_1”, “item_2”, … Read more

Count the Number of Times an Item Appears in a Python List

Here are 3 ways to count the number of times an item appears in a Python list: (1) Using count() to count a specific item. For example, let’s count the number of times “mm” appears in a list: Copy my_list = [“mm”, “bb”, “cc”, “mm”, “mm”, “cc”]count_item = my_list.count(“mm”)print(count_item) The result is 3 times: (2) … Read more

Replace All Instances of Characters in a String – Python

To replace all instances of characters in a string in Python: Copy final_string = original_string.replace(“original_value”, “new_value”) And if you want to replace n number of instances: Copy final_string = original_string.replace(“original_value”, “new_value”, n) Examples of Replacing Instances of Characters in a String in Python Example 1: Simple replacement of instances in a string Assume that you … Read more

Convert String to List in Python

To convert a string to a list in Python: Copy my_list = my_string.split(“separated_by”) For example, if your string is separated by spaces, then apply this syntax to convert the string with the spaces into a list (notice that a space was applied inside the brackets of the “split”): Copy my_string = “aa bb cc dd … Read more

Convert List to String in Python

Here are different ways to convert a list to a string in Python. Note that if your list contains numeric data, you’ll first need to convert each element in the list to a string (as captured in points 3 and 4 below). (1) Convert a list that contains text to a string (with spaces among … Read more

How to Export Pandas Series to a CSV File

Here are 4 approaches to export Pandas Series to a CSV file: (1) No index and no header Copy import pandas as pdser = pd.Series([“value_1”, “value_2”, “value_3″, …])ser.to_csv(r”Path to store the CSV file\File Name.csv”, index=False, header=False) (2) No index but with header Copy import pandas as pdser = pd.Series([“value_1”, “value_2”, “value_3”, …])ser.rename(“header name”, inplace=True)ser.to_csv(r”Path to … Read more

How to Import a Text File into R

To import a text file into R: Copy read.csv(“Path where the TXT file is stored\\File Name.txt”) Steps to Import a Text file into R Step 1: Prepare the text file To start, prepare a simple text file (called ‘Products‘) with the following data: product,pricecomputer,800monitor,450keyboard,100printer,150tablet,300 Step 2: Capture the path of the file Next, capture the … Read more

How to Export a DataFrame to a Text File in R

To export a DataFrame to a text file in R: Copy write.table(df, “Path to export the DataFrame\\File Name.txt”) Add a separator, such as a comma separator: Copy write.table(df, “Path to export the DataFrame\\File Name.txt”, sep=”,”) Exclude the row numbers: Copy write.table(df, “Path to export the DataFrame\\File Name.txt”, row.names=FALSE) Omit the quotes: Copy write.table(df, “Path to … Read more

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