Category "list-comprehension"

Improving (shortening) my list comprehension with lambda x function?

!!! I know I could use Counter() here, but that's not the point !!! Giving the code below: test =[['a',1],['a',2],['b',3], ['c',5],['a',7],['c',9]] {i:[test[x]

Decouple dictionary that contains a list as a value for a particular key

Let's say I have a dict in Python that follows this structure: dict_a = {"a": [1,2,3]} I want to produce an output to 'decouple' dict_a in a list of 3 separate

Filling a 2D array with values from a 1D array, but using list comprehension

I'm trying to "paste" the values contained in the 1D array dataslice (1x8) into the 2D array values_matrix (9x9), following the position indices contained in th

How to add an extra middle step into a list comprehension?

Let's say I have a list[str] object containing timestamps in "HH:mm" format, e.g. timestamps = ["22:58", "03:11", "12:21"] I want to convert it to a list[int]

Pythonic way to print list items

I would like to know if there is a better way to print all objects in a Python list than this : myList = [Person("Foo"), Person("Bar")] print("\n".join(map(str

Python: Generating all n-length arrays combinations of values within a range

Ok. I'm looking for the smartest and more compact way to do this function def f(): [[a,b,c] for a in range(6) for b in range(6) for c in range(6)] which

Assign the result of a loop to a variable in Python

Consider a list I want to parse using a for : friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"] for i in friends: print i will retur

Can list comprehensions make groups?

I have a list that looks like [(1,2,5),(2,10,13),(5,24,56),(1,8,10),(2,3,11)] How can I produce a dictionary by grouping by first element of tuples and find

How to find list comprehension in python code

I want to find a list comprehension in python source code, for that I tried to use Pygments, but it didn't find the way to do that. To be more specific, I wan

I want to speed up a nested loop when creating a df keyword counts (keywords appearing with other keywords)

Using Pandas I have a df that is 14000 rows by 56 columns (keywords) I have a keyword list (full_keys) that is 1406 items and an empty (0) dataframe (called key

List comprehension: Returning two (or more) items for each item

Is it possible to return 2 (or more) items for each item in a list comprehension? What I want (example): [f(x), g(x) for x in range(n)] should return [f(0),

Python: Printing a list with a line break after every nth item

I have a list of media that I want to print in the following format, with a line break after every nth item: media_list = ['A001', 'A002', 'A003', 'A004', 'A00

Line continuation for list comprehensions or generator expressions in python

How are you supposed to break up a very long list comprehension? [something_that_is_pretty_long for something_that_is_pretty_long in somethings_that_are_pretty