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 …

Total answers: 1

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 …

Total answers: 3

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 …

Total answers: 1

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 …

Total answers: 1

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 …

Total answers: 2

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 …

Total answers: 4

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 …

Total answers: 1

list comprehension doesn't work with input

list comprehension doesn't work with input Question: I am trying to make a plan that allows you to insert strings to a list until you type a certain keyword which then locks the list from further appending. print("I will list everything you desire!") list = [] while input("") != "stop": shop_list = [list.append(i) for i …

Total answers: 1

Search part of tuple list

Search part of tuple list Question: I am trying to do a list comprehension of a tuples list based on another list of tuples with partial match. x = [((1,1),(1,1),(1,2)), ((2,1),(1,3),(2,9)), ((2,1),(2,3),(2,9))] y = [(2,1),(1,3)] [i for i in x for k in y if k in i] e.g. in this x is a list …

Total answers: 3