list-comprehension Questions
6
Solved
I have a list of dictionaries such as:
[{'mykey1':'myvalue1', 'mykey2':'myvalue2'}, {'mykey1':'myvalue1a', 'mykey2':'myvalue2a'}]
I need to remove all key values pairs from all dictionaries wher...
Flocculent asked 6/11, 2012 at 15:30
2
Efficiently filtering out 'None' items in a list comprehension in which a function is called
I have a list comprehension that calls a function which potentially returns None.
f = lambda x: x if x < 3 else None
l = [f(x) for x in [1,2,3,4]] # [1, 2, None, None]
I'd like to have the list...
Grandfather asked 4/2, 2018 at 15:44
18
Solved
I have a list A, and a function f which takes an item of A and returns a list. I can use a list comprehension to convert everything in A like [f(a) for a in A], but this returns a list of lists. Su...
Solubilize asked 2/7, 2009 at 22:40
14
Solved
I am new to python, and I was wondering if I could generate the fibonacci series using python's list comprehension feature. I don't know how list comprehensions are implemented.
I tried the follow...
Overcast asked 21/2, 2017 at 14:44
6
Solved
I have the following code:
[x ** 2 for x in range(10)]
When I run it in the Python shell, it returns:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
I've searched and it seems this is called a list compreh...
Discant asked 17/1, 2016 at 6:55
19
Solved
I am trying to create a dictionary from a csv file. The first column of the csv file contains unique keys and the second column contains values. Each row of the csv file represents a unique key, va...
Doorpost asked 19/7, 2011 at 0:9
1
Solved
Gurobipy can apparently read the index of a list comprehension formulated within the parentheses of a function. How does this work? Shouldn't this formulation pass a generator object to the functio...
Aileen asked 22/1, 2024 at 11:14
4
Solved
I want to create a list of widgets MyWidget(categoryName, color) from the following two lists.
static const _categoryNames = <String>[
'Length',
'Area',
'Volume',
];
static const _ba...
Additament asked 6/6, 2020 at 15:49
7
Solved
I'm a Python newbie and one of the things I am trying to do is wrap my head around list comprehension. I can see that it's a pretty powerful feature that's worth learning.
cities = ['Chicago', 'De...
Yawp asked 30/1, 2010 at 22:3
3
Solved
This is disassembly of a list comprehension in python-3.10:
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or &...
Gentille asked 16/11, 2023 at 12:53
13
Solved
How do I convert the following for-loop containing an if/else into a list comprehension?
results = []
for x in xs:
results.append(f(x) if x is not None else '')
It should yield '' if x is None, a...
Stoss asked 23/11, 2010 at 19:56
10
Solved
Is it possible to have something like:
list1 = ...
currentValue = 0
list2 = [currentValue += i, i for i in list1]
I tried that but didn't work? What's the proper syntax to write those?
EDIT: t...
Medeah asked 21/4, 2009 at 22:14
10
Solved
How can I break a list comprehension based on a condition, for instance when the number 412 is found?
Code:
numbers = [951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
6...
Admetus asked 5/3, 2012 at 19:40
2
Solved
Let's say I have a list datalist, with len(datalist) = 4. Let's say I want each of the elements in the list to be represented in a string in this way:
s = "'{0}'::'{1}'::'{2}' '{3}'\n".format(data...
Jegger asked 23/8, 2017 at 20:33
5
Solved
Python has syntactically sweet list comprehensions:
S = [x**2 for x in range(10)]
print S;
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
In PHP I would need to do some looping:
$output = array();
$Nums ...
Inpatient asked 12/8, 2009 at 15:26
3
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 want to do a function that recognize all...
Repellent asked 2/2, 2016 at 9:47
3
Solved
Are for loops really "bad"? If not, in what situation(s) would they be better than using a more conventional "vectorized" approach?1
I am familiar with the concept of "vectorization", and how pand...
Corvus asked 3/1, 2019 at 18:54
6
Solved
I'm trying to think of a one-liner to achieve the following (summing all the values of a key):
>>> data = [('a', 1), ('b', 3), ('a', 4), ('c', 9), ('b', 1), ('d', 3)]
>>> res = {}...
Sarabia asked 14/1, 2014 at 11:48
6
Solved
I have a list word_list = ['cat', 'dog', 'rabbit'].
I want to use list comprehension to print each individual character from the list but removes any duplicate character. This is my code:
word_list...
Footway asked 29/8, 2023 at 0:56
7
Solved
I have a list and want to build (via a comprehension) another list. I would like this new list to be limited in size, via a condition
The following code will fail:
a = [1, 2, 1, 2, 1, 2]
b = [i f...
Krusche asked 22/2, 2017 at 13:57
3
Solved
There are a great many existing Q&A on Stack Overflow on this general theme, but they are all either poor quality (typically, implied from a beginner's debugging problem) or miss the mark in so...
Cristicristian asked 7/3, 2023 at 19:30
9
Solved
How do you access other class variables from a list comprehension within the class definition? The following works in Python 2 but fails in Python 3:
class Foo:
x = 5
y = [x for i in range(1)]
P...
Cohe asked 16/12, 2012 at 21:42
10
Solved
I want to create a series of lists, all of varying lengths. Each list will contain the same element e, repeated n times (where n = length of the list).
How do I create the lists, without using a li...
Micropyle asked 11/8, 2010 at 14:1
14
Solved
I want to generate a list in python as follows -
[1, 1, 2, 4, 3, 9, 4, 16, 5, 25 .....]
You would have figured out, it is nothing but n, n*n
I tried writing such a list comprehension in python...
Podophyllin asked 16/5, 2013 at 18:33
6
Solved
I'm new to Python and is confused by a piece of code in Python's official documentation.
unique_words = set(word for line in page for word in line.split())
To me, it looks equivalent to:
unique...
Candlepin asked 5/11, 2014 at 14:13
1 Next >
© 2022 - 2025 — McMap. All rights reserved.