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
512
votes
13 answers

Why is there no tuple comprehension in Python?

As we all know, there's list comprehension, like [i for i in [1, 2, 3, 4]] and there is dictionary comprehension, like {i:j for i, j in {1: 'a', 2: 'b'}.items()} but (i for i in (1, 2, 3)) will end up in a generator, not a tuple comprehension.…
101
votes
3 answers

Python Set Comprehension

So I have these two problems for a homework assignment and I'm stuck on the second one. Use a Python Set Comprehension (Python's equivalent of Set Builder notation) to generate a set of all of the prime numbers that are less than 100. Recall that…
user3308790
  • 1,013
  • 2
  • 7
  • 5
22
votes
1 answer

Dictionary/set comprehensions inside of f-string

Is it possible to have a dictionary or set comprehension inside of an f-string in python 3.6+? It seems syntactically impossible: names = ['a', 'b', 'c'] pks = [1, 2, 3] f"{{name : pk for name, pk in zip(names, pks)}}" This will return: {name :…
14
votes
1 answer

How do python Set Comprehensions work?

Q1 - Is the following a set() of a generator expression or a set comprehension? (Or are they same? If so, are list & dict comprehensions also corresponding type-cast on generators?) my_set = {x for x in range(10)} Q2 - Does the evaluation consider…
shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
7
votes
2 answers

python set comprehension for 2.6

I was trying set comprehension for 2.6, and came across the following two ways. I thought the first method would be faster than the second, timeit suggested otherwise. Why is the second method faster even though the second method has got an extra…
Anandan
  • 319
  • 3
  • 10
4
votes
2 answers

Python Set Comprehension Nested in Dict Comprehension

I have a list of tuples, where each tuple contains a string and a number in the form of: [(string_1, num_a), (string_2, num_b), ...] The strings are nonunique, and so are the numbers, e.g. (string_1 , num_m) or (string_9 , num_b) are likely to…
3
votes
2 answers

What is the most efficient way to get a list/set of keys from dictionary in python?

In order to quickly compare the keys of 2 dictionaries, I'm creating sets of the keys using this method: dict_1 = {"file_1":10, "file_2":20, "file_3":30, "file_4":40} dict_2 = {"file_1":10, "file_2":20, "file_3":30} set_1 = {file for file in…
Asi
  • 31
  • 4
3
votes
5 answers

How to get keys from the inner dictionaries of a nested dictionary using comprehension?

I am trying to find the keys of the dictionaries inside a dictionary and write them into a set using set/list comprehension. So it looks like this: dict_o_dicts = { 1: {'de': 'eins', 'en': 'one' }, 2: {'de': 'zwei', 'en': 'two' }, 3:…
3
votes
3 answers

How to get listcomprehension result as unpacked list

I have a function (in the example: some_function()) that returns a set. I got a data structure of some elements (in the example arr) and need to map the elements to the function and I want to get back a set of all elements. Not a set of sets but a…
mxcxx
  • 33
  • 3
3
votes
1 answer

set comprehension not behaving as expected

Need help on why this code snippet does not return as I'd expect >>> a = 1 >>> v = ["a", "b", "c"] >>> {e for e in v if locals().get(e) is None} set(['a', 'c', 'b']) I expected it to return set(['c', 'b']), just like if I build a list >>> [e for e…
zhaomin
  • 381
  • 3
  • 13
3
votes
2 answers

Obtain a set of unique values from a dictionary of lists

I have a list of dictionaries in which the dictionaries also contain a list. I want to generate a set of the values of the respective nested lists so that I end up with a set of all of the unique items (in this case, hobbies). I feel a set is…
MarkS
  • 1,455
  • 2
  • 21
  • 36
2
votes
2 answers

Get single "set" object from the list values present in the dictionary

I'm trying to build a set from the values of a dictionary. Each dictionary value is a list of strings. {'a': ['a','b','c'],'b':['a','b','d'],...} I am trying to use .update(x) to concatenate a set containing values from the dictionary. I already…
2
votes
2 answers

Union of multiple sets in a list

This is my code. s = set() for x in [ {1,2}, {3,4}, {5,1} ]: s |= x It returns set([1, 2, 3, 4, 5]). Is it possible to use set comprehension in such a case? How can I write it shorter?
hardfork
  • 2,470
  • 1
  • 23
  • 43
2
votes
3 answers

Most efficient way to get values from a dictionary into a set

I cant seem to figure out how to write the following into a one line of code, using set comprehension. If its not possible is there a faster way to do what I am trying to do here? Basically just getting all the values of a dictionary into a set. …
user1179317
  • 2,693
  • 3
  • 34
  • 62
2
votes
1 answer

How does this for-loop within this dictionary work exactly?

Currently I'm learning Python text sentiment module via this online course and the lecturer failed to explain in enough detail how this piece of code works. I tried searching each piece of code individually to try piece together how he did it but it…
SeyiA
  • 95
  • 6
1
2 3 4