Questions tagged [set-comprehension]

A syntactic construct which provides a concise way to create sets in a style similar to the mathematical set-builder notation.

Set comprehensions can be used to construct sets in one line. For simple tasks, a set comprehension may be more readable than sets built using looping constructs.

Set comprehensions tend to consist of an input sequence of either a list, a dictionary or a tuple, variable bindings, filtering predicates and an output expression.

Set comprehensions are very similar to list comprehensions.

57 questions
0
votes
1 answer

"Double loop" set comprehension

I know I am missing something obvious, but I am lost in how to make this one work. I am trying to write the set comprehension for a kind of "Cartesian product". Given a finite set S of finite sets, I would like to build a sets of sets of pairs,…
Alicia M.
  • 7
  • 3
0
votes
3 answers

Set Comprehensions in Python

I came across this code and didn't quite really understand it: s = { i*j for i in range(10) for j in range(10)} print(s) The result is {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, 28, 30, 32, 35, 36, 40, 42, 45, 48,…
0
votes
2 answers

How to get unique values from a list of dictionaries?

I have a list of dictionaries: dicts = [{'year':2020, 'target':'usd'}, {'year':2019, 'target':'eur'}, {'year':2018, 'target':'eur'}, {'year':2018, 'target':'eur'}, {'year':2016, 'target':'eur'}] and I want to list the unique years only, how can I…
0
votes
2 answers

Unchanging Variable in list comprehension

I have a list like this: dates = [ datetime.date(2014, 11, 24), datetime.date(2014, 11, 25), datetime.date(2014, 11, 26), # datetime.date(2014, 11, 27), # This one is missing datetime.date(2014, 11, 28), datetime.date(2014,…
v1z3
  • 137
  • 2
  • 9
0
votes
0 answers

Is it Pythonic to use set comprehensions for just side effects?

Question related to this which asks about side effects in list comprehensions. def side_effects_only(x): # ...side effects... return # None used as {side_effects_only(x) for x in Y} # where Y is large... This shouldn't generate some giant…
k1m190r
  • 1,213
  • 15
  • 26
0
votes
3 answers

If-for-else nested set comprehension in python

I'm trying to convert the following nested conditions to set comprehension, but couldn't get it working properly. processed = set() if isinstance(statements, list): for action in statements: processed.add(action) else: …
Prakash
  • 601
  • 2
  • 9
  • 24
0
votes
1 answer

Reading lines from a file using a generator comprehension vs a list comprehension

The below code is from chapter 3 of Python Data Science Handbook by Jake VanderPlas. Each line in the file is a valid JSON. While I don't think the specifics of the file are critical to answering this question, the url for the file is…
jgg
  • 791
  • 4
  • 17
0
votes
3 answers

Python : comprehension list, square brackets vs list()

Hello I could not found the difference between using square brackets for comprehension list versus using list() Is there a performance/ memory allocation difference ? ( same question for set and dict ) input = [1, 2, 3, 4] B = [a * 2 for a in input…
0
votes
2 answers

How would I write this using set comprehension?

It works using a regular loop but I want it to work using set comprehension. def setComp(): result = set() for n in range(1, 101): x = n y = x**2 if y%x == 0 and y%3 == 0: tup = (x,y) …
user7619593
0
votes
1 answer

Limit the size of a set comprehension from an input

Is there a way to limit the size of set comprehension when parsing an input. Here's a simple example: import sys values = {x.strip() for x in open(sys.argv[1], 'r')} print(values) The size of values is unbounded, but is there a way to limit it?…
Dave Johansen
  • 889
  • 1
  • 7
  • 23
0
votes
0 answers

time complexity of set comprehension vs tuple comprehension vs list comprehension

I am learning python and was checking out the timeit module. I am trying the exact same thing with the list, set and tuple to check the overheads involved. Please take a gander (IPython shell) :- >>> timeit {x**9 for x in range(100)} >>> 49.3 µs ±…
Gsbansal10
  • 303
  • 5
  • 12
0
votes
2 answers

How to use parallelization in set/list comprehension using asyncio?

I want to create a multiprocess comprehension in Python 3.7. Here's the code I have: async def _url_exists(url): """Check whether a url is reachable""" request = requests.get(url) return request.status_code == 200: async def…
0
votes
3 answers

set of keys for nested dictionaries

I have a couple lines to populate a set. x = {1: {2: 4, 3: 6}, 5: {2:6, 10: 25, 14: 12}} keys = set() for y in x: for z in x[y]: keys.add(z) # keys is now `set([2, 3, 10, 14])` I can't shake the feeling that I can do this better, but…
Michael
  • 8,362
  • 6
  • 61
  • 88
0
votes
1 answer

Slow pairwise comparison

I have a code that open two files, save their contents to sets (set1 and set2) and save the results of a pairwise comparison between these sets to an output file. Both files are really big (more than 100K lines each) and this code is taking a long…
Marcos Santana
  • 911
  • 5
  • 12
  • 21
0
votes
0 answers

Trouble understanding Set comprehensions

I don't quite understand why their is a C in the beginning of the {}. Is this just syntax for comprehensive sets? x= {c for c in "I've got plenty of nothing"} print(sorted(x)) This prints: [' ', "'", I, 'e', 'f', 'g', 'h', 'i', 'l', 'n', 'o', 'p',…
Gabe
  • 9