# set

## Why does "set(a) and set(b)" vs "set(a) & set(b)" give different results in python?

Why does "set(a) and set(b)" vs "set(a) & set(b)" give different results in python? Question: I understand that one is bitwise operation while the other is not, but how does this bitwise property affect the results of given expression. As both should simply compare the elements in both sets and result in common elements. set(a) …

## Make set keep order of a tuple

Make set keep order of a tuple Question: In the code below, how can I use keep using set to have unique values, but also make set keep the order of how the filters are typed in? >>> filters = (‘f1’, ‘f2’) >>> set((*filters, ‘f3’)) {‘f1’, ‘f3’, ‘f2’} # expected: {‘f1’, ‘f2’, ‘f3’} Asked By: …

## remove an element from a set and return the set

remove an element from a set and return the set Question: I need to iterate over a copy of a set that doesn’t contain a certain element. Until now I’m doing this: for element in myset: if element != myelement: … But I want something like this, in one line: for element in myset.copy().remove(myelement): … …

## Python set iteration time complexity

Python set iteration time complexity Question: What is the time complexity of the following python code? create set x add n items in x remove n items in x add 1 item in x iterate x m times import time def test(n, m): answer = 0 start = time.time() x = set([i for i in …

## How to create sets for a column, separated by data types?

How to create sets for a column, separated by data types? Question: I want to create a set in which only the string values of a column appear. The numerical values of the column are to be stored in another set. Asked By: di1a || Source Answers: Try this: my_dataset = [ {‘id’: 1, ‘my_column’: …

## problem with itertools permutations in combination with english_words_set in a for loop

problem with itertools permutations in combination with english_words_set in a for loop Question: I want to get all possible words with 5,6,7 characters from a random string: word="ualargd" I do the following: A for loop to change the length of my permutations. for i in range(5,len(word)+1): mywords=permutations(word, i) The items created by the permutation get …

## set() in Python

set() in Python Question: I have used the set() function but I am confused. x = set("car") print(x) Why does this code output: "a", "c", "r" and not "car"? Thanks for your answers. Asked By: Michal Dostal || Source Answers: It’s because of the main properties of set in Python. They can’t contain duplicates They’re …

## None to Empty List

None to Empty List Question: I have to find the sum of two that are equivalent to value s, can’t use dictionaries, only sets, arrays, and lists. If none, then I have to return an empty list. This is my code, I tried to remove None using if statement, but it didn’t work, not sure …