# list-comprehension

## Python index selection in a 3D list

Python index selection in a 3D list Question: I have the following 3D list: test = [[[(x,y,z) for x in range(0,5)] for y in range(5,8)] for z in range(0,4)] test[0].append([(0,5),(5,0)]) I want to select all the indices of the first dimension, the 0th index of the 2nd dimension and all the indices of the 3rd …

## Can a list comprehension be divided in two lists?

Can a list comprehension be divided in two lists? Question: I think I’ve caught the idea of one-line for loop, but now I have a problem. I know I can define a dataframe column using this like: df = pd.DataFrame(columns=["columnA"]) list = [0, 1, 2, 3, 4] df["columnA"] = [i for i in list] Now …

## Read a file and generate a list of lists using list comprehensions

Read a file and generate a list of lists using list comprehensions Question: I’d like to read a file with the following input: 10 20 30 50 60 70 80 90 100 and generate the following output: [[’10’, ’20’, ’30’], [’50’,’60’,’70’] … ] using list comprehensions and not foor loops. Naturally the problem I’m facing …

## How to filter for substring in list comprehension in python

How to filter for substring in list comprehension in python Question: I have a dictionary of dataframes. I want to create a list of the names of all of the dataframes in this dictionary that have the substring "blue" in them. No dataframes in the dictionary of dataframes contain a column simply called "blue". It …

## Function in Python list comprehension, don't eval twice

Function in Python list comprehension, don't eval twice Question: I’m composing a Python list from an input list run through a transforming function. I would like to include only those items in the output list for which the result isn’t None. This works: def transform(n): # expensive irl, so don’t execute twice return None if …

## handy way to get rid of some fields using list comprehension

handy way to get rid of some fields using list comprehension Question: So, imagine an item like so: x = {"name":"blah", "this_must_go":"obsolette", "this_must_also_go":"unfixable", "type":4} and I have lets say a list of 200 of these xes and I want to remove all this_must_go and this_must_also_go fields from all x in the list, no exception. I …

## How to get the sum of elements in two different lists in a DataFrame instead of concatenation in Python?

How to get the sum of elements in two different lists in a DataFrame instead of concatenation in Python? Question: I have a DataFrame that contains two columns, ‘A_List’ and ‘B_List’, which are of the string dtype. I have converted these to lists and I would like to now perform element wise addition of the …

## Appending all other key:values in a specific dictionary in Python

Appending all other key:values in a specific dictionary in Python Question: I am trying to create a list of all actors’ co-actors. The exemplary input looks like this: cast_info = [[ {‘name’: ‘Drew Barrymore’}, {‘name’: ‘Brian Herzlinger’}, {‘name’: ‘Corey Feldman’}, {‘name’: ‘Eric Roberts’}, {‘name’: ‘Griffin Dunne’}, {‘name’: ‘Samuel L. Jackson’}, {‘name’: ‘Matt LeBlanc’}, {‘name’: "Bill …