Add list to set - python

How do I add a list of values to an existing set?

Adding the contents of a list
Use set.update() or the |= operator:
>>> a = set('abc')
>>> a
{'a', 'b', 'c'}
>>> xs = ['d', 'e']
>>> a.update(xs)
>>> a
{'e', 'b', 'c', 'd', 'a'}
>>> xs = ['f', 'g']
>>> a |= set(xs)
>>> a
{'e', 'b', 'f', 'c', 'd', 'g', 'a'}
Adding the list itself
It is not possible to directly add the list itself to the set, since set elements must be hashable.
Instead, one may convert the list to a tuple first:
>>> a = {('a', 'b', 'c')}
>>> xs = ['d', 'e']
>>> a.add(tuple(xs))
>>> a
{('a', 'b', 'c'), ('d', 'e')}

You can't add a list to a set because lists are mutable, meaning that you can change the contents of the list after adding it to the set.
You can however add tuples to the set, because you cannot change the contents of a tuple:
>>> a.add(('f', 'g'))
>>> print a
set(['a', 'c', 'b', 'e', 'd', ('f', 'g')])
Edit: some explanation: The documentation defines a set as an unordered collection of distinct hashable objects. The objects have to be hashable so that finding, adding and removing elements can be done faster than looking at each individual element every time you perform these operations. The specific algorithms used are explained in the Wikipedia article. Pythons hashing algorithms are explained on effbot.org and pythons __hash__ function in the python reference.
Some facts:
Set elements as well as dictionary keys have to be hashable
Some unhashable datatypes:
list: use tuple instead
set: use frozenset instead
dict: has no official counterpart, but there are some
recipes
Object instances are hashable by default with each instance having a unique hash. You can override this behavior as explained in the python reference.

To add the elements of a list to a set, use update
From https://docs.python.org/2/library/sets.html
s.update(t): return set s with elements added from t
E.g.
>>> s = set([1, 2])
>>> l = [3, 4]
>>> s.update(l)
>>> s
{1, 2, 3, 4}
If you instead want to add the entire list as a single element to the set, you can't because lists aren't hashable. You could instead add a tuple, e.g. s.add(tuple(l)). See also TypeError: unhashable type: 'list' when using built-in set function for more information on that.

Hopefully this helps:
>>> seta = set('1234')
>>> listb = ['a','b','c']
>>> seta.union(listb)
set(['a', 'c', 'b', '1', '3', '2', '4'])
>>> seta
set(['1', '3', '2', '4'])
>>> seta = seta.union(listb)
>>> seta
set(['a', 'c', 'b', '1', '3', '2', '4'])

Please notice the function set.update(). The documentation says:
Update a set with the union of itself and others.

list objects are unhashable. you might want to turn them in to tuples though.

Sets can't have mutable (changeable) elements/members. A list, being mutable, cannot be a member of a set.
As sets are mutable, you cannot have a set of sets!
You can have a set of frozensets though.
(The same kind of "mutability requirement" applies to the keys of a dict.)
Other answers have already given you code, I hope this gives a bit of insight.
I'm hoping Alex Martelli will answer with even more details.

I found I needed to do something similar today. The algorithm knew when it was creating a new list that needed to added to the set, but not when it would have finished operating on the list.
Anyway, the behaviour I wanted was for set to use id rather than hash. As such I found mydict[id(mylist)] = mylist instead of myset.add(mylist) to offer the behaviour I wanted.

You want to add a tuple, not a list:
>>> a=set('abcde')
>>> a
set(['a', 'c', 'b', 'e', 'd'])
>>> l=['f','g']
>>> l
['f', 'g']
>>> t = tuple(l)
>>> t
('f', 'g')
>>> a.add(t)
>>> a
set(['a', 'c', 'b', 'e', 'd', ('f', 'g')])
If you have a list, you can convert to the tuple, as shown above. A tuple is immutable, so it can be added to the set.

You'll want to use tuples, which are hashable (you can't hash a mutable object like a list).
>>> a = set("abcde")
>>> a
set(['a', 'c', 'b', 'e', 'd'])
>>> t = ('f', 'g')
>>> a.add(t)
>>> a
set(['a', 'c', 'b', 'e', 'd', ('f', 'g')])

Here is how I usually do it:
def add_list_to_set(my_list, my_set):
[my_set.add(each) for each in my_list]
return my_set

Try using * unpack, like below:
>>> a=set('abcde')
>>> a
{'a', 'd', 'e', 'b', 'c'}
>>> l=['f','g']
>>> l
['f', 'g']
>>> {*l, *a}
{'a', 'd', 'e', 'f', 'b', 'g', 'c'}
>>>
Non Editor version:
a=set('abcde')
l=['f', 'g']
print({*l, *a})
Output:
{'a', 'd', 'e', 'f', 'b', 'g', 'c'}

Union is the easiest way:
list0 = ['a', 'b', 'c']
set0 = set()
set0.add('d')
set0.add('e')
set0.add('f')
set0 = set0.union(list0)
print(set0)
Output:
{'b', 'd', 'f', 'c', 'a', 'e'}

Related

Python indirect list indexing [duplicate]

In Python I have a list of elements aList and a list of indices myIndices. Is there any way I can retrieve all at once those items in aList having as indices the values in myIndices?
Example:
>>> aList = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> myIndices = [0, 3, 4]
>>> aList.A_FUNCTION(myIndices)
['a', 'd', 'e']
I don't know any method to do it. But you could use a list comprehension:
>>> [aList[i] for i in myIndices]
Definitely use a list comprehension but here is a function that does it (there are no methods of list that do this). This is however bad use of itemgetter but just for the sake of knowledge I have posted this.
>>> from operator import itemgetter
>>> a_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> my_indices = [0, 3, 4]
>>> itemgetter(*my_indices)(a_list)
('a', 'd', 'e')
Indexing by lists can be done in numpy. Convert your base list to a numpy array and then apply another list as an index:
>>> from numpy import array
>>> array(aList)[myIndices]
array(['a', 'd', 'e'],
dtype='|S1')
If you need, convert back to a list at the end:
>>> from numpy import array
>>> a = array(aList)[myIndices]
>>> list(a)
['a', 'd', 'e']
In some cases this solution can be more convenient than list comprehension.
You could use map
map(aList.__getitem__, myIndices)
or operator.itemgetter
f = operator.itemgetter(*aList)
f(myIndices)
If you do not require a list with simultaneous access to all elements, but just wish to use all the items in the sub-list iteratively (or pass them to something that will), its more efficient to use a generator expression rather than list comprehension:
(aList[i] for i in myIndices)
Alternatively, you could go with functional approach using map and a lambda function.
>>> list(map(lambda i: aList[i], myIndices))
['a', 'd', 'e']
I wasn't happy with these solutions, so I created a Flexlist class that simply extends the list class, and allows for flexible indexing by integer, slice or index-list:
class Flexlist(list):
def __getitem__(self, keys):
if isinstance(keys, (int, slice)): return list.__getitem__(self, keys)
return [self[k] for k in keys]
Then, for your example, you could use it with:
aList = Flexlist(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
myIndices = [0, 3, 4]
vals = aList[myIndices]
print(vals) # ['a', 'd', 'e']

why cant we add lists or dictionaries or tuples inside set [duplicate]

How do I add a list of values to an existing set?
Adding the contents of a list
Use set.update() or the |= operator:
>>> a = set('abc')
>>> a
{'a', 'b', 'c'}
>>> xs = ['d', 'e']
>>> a.update(xs)
>>> a
{'e', 'b', 'c', 'd', 'a'}
>>> xs = ['f', 'g']
>>> a |= set(xs)
>>> a
{'e', 'b', 'f', 'c', 'd', 'g', 'a'}
Adding the list itself
It is not possible to directly add the list itself to the set, since set elements must be hashable.
Instead, one may convert the list to a tuple first:
>>> a = {('a', 'b', 'c')}
>>> xs = ['d', 'e']
>>> a.add(tuple(xs))
>>> a
{('a', 'b', 'c'), ('d', 'e')}
You can't add a list to a set because lists are mutable, meaning that you can change the contents of the list after adding it to the set.
You can however add tuples to the set, because you cannot change the contents of a tuple:
>>> a.add(('f', 'g'))
>>> print a
set(['a', 'c', 'b', 'e', 'd', ('f', 'g')])
Edit: some explanation: The documentation defines a set as an unordered collection of distinct hashable objects. The objects have to be hashable so that finding, adding and removing elements can be done faster than looking at each individual element every time you perform these operations. The specific algorithms used are explained in the Wikipedia article. Pythons hashing algorithms are explained on effbot.org and pythons __hash__ function in the python reference.
Some facts:
Set elements as well as dictionary keys have to be hashable
Some unhashable datatypes:
list: use tuple instead
set: use frozenset instead
dict: has no official counterpart, but there are some
recipes
Object instances are hashable by default with each instance having a unique hash. You can override this behavior as explained in the python reference.
To add the elements of a list to a set, use update
From https://docs.python.org/2/library/sets.html
s.update(t): return set s with elements added from t
E.g.
>>> s = set([1, 2])
>>> l = [3, 4]
>>> s.update(l)
>>> s
{1, 2, 3, 4}
If you instead want to add the entire list as a single element to the set, you can't because lists aren't hashable. You could instead add a tuple, e.g. s.add(tuple(l)). See also TypeError: unhashable type: 'list' when using built-in set function for more information on that.
Hopefully this helps:
>>> seta = set('1234')
>>> listb = ['a','b','c']
>>> seta.union(listb)
set(['a', 'c', 'b', '1', '3', '2', '4'])
>>> seta
set(['1', '3', '2', '4'])
>>> seta = seta.union(listb)
>>> seta
set(['a', 'c', 'b', '1', '3', '2', '4'])
Please notice the function set.update(). The documentation says:
Update a set with the union of itself and others.
list objects are unhashable. you might want to turn them in to tuples though.
Sets can't have mutable (changeable) elements/members. A list, being mutable, cannot be a member of a set.
As sets are mutable, you cannot have a set of sets!
You can have a set of frozensets though.
(The same kind of "mutability requirement" applies to the keys of a dict.)
Other answers have already given you code, I hope this gives a bit of insight.
I'm hoping Alex Martelli will answer with even more details.
I found I needed to do something similar today. The algorithm knew when it was creating a new list that needed to added to the set, but not when it would have finished operating on the list.
Anyway, the behaviour I wanted was for set to use id rather than hash. As such I found mydict[id(mylist)] = mylist instead of myset.add(mylist) to offer the behaviour I wanted.
You want to add a tuple, not a list:
>>> a=set('abcde')
>>> a
set(['a', 'c', 'b', 'e', 'd'])
>>> l=['f','g']
>>> l
['f', 'g']
>>> t = tuple(l)
>>> t
('f', 'g')
>>> a.add(t)
>>> a
set(['a', 'c', 'b', 'e', 'd', ('f', 'g')])
If you have a list, you can convert to the tuple, as shown above. A tuple is immutable, so it can be added to the set.
You'll want to use tuples, which are hashable (you can't hash a mutable object like a list).
>>> a = set("abcde")
>>> a
set(['a', 'c', 'b', 'e', 'd'])
>>> t = ('f', 'g')
>>> a.add(t)
>>> a
set(['a', 'c', 'b', 'e', 'd', ('f', 'g')])
Here is how I usually do it:
def add_list_to_set(my_list, my_set):
[my_set.add(each) for each in my_list]
return my_set
Try using * unpack, like below:
>>> a=set('abcde')
>>> a
{'a', 'd', 'e', 'b', 'c'}
>>> l=['f','g']
>>> l
['f', 'g']
>>> {*l, *a}
{'a', 'd', 'e', 'f', 'b', 'g', 'c'}
>>>
Non Editor version:
a=set('abcde')
l=['f', 'g']
print({*l, *a})
Output:
{'a', 'd', 'e', 'f', 'b', 'g', 'c'}
Union is the easiest way:
list0 = ['a', 'b', 'c']
set0 = set()
set0.add('d')
set0.add('e')
set0.add('f')
set0 = set0.union(list0)
print(set0)
Output:
{'b', 'd', 'f', 'c', 'a', 'e'}

Alternative to using the sort function when adding to a list?

I want to insert a word alphabetically into a list. Originally I would append the word I'm adding to the end of the list and then sort the list, but I am not allowed to use the sort() function.
Is there a way to do this through a function?
Based of of #SheshankS.'s answer. A function to do this for you:
def insert(item, _list):
for index, element in enumerate(_list):
if item < element: # in python, this automatically compares alphabetical precedence.
_list.insert(index, item)
return # exit out of the function since we already inserted
# if the item was not inserted, it must have the lowest precedence, so just append it
_list.append(item)
Note that since lists are mutable, this will actually mutate the given instance.
So, this:
someList = ["a", "b", "d"]
insert("c", someList)
Will actually change someList instead of just returning the new value.
Try doing this:
array = ["asdf", "bsdf", "kkkk", "zssdd"]
insertion_string = "zzat"
i = 0
for element in array:
if insertion_string < element:
array.insert(i, insertion_string)
break
i += 1
# if it is last one
if not insertion_string in array:
array.append(insertion_string)
print (array )
Repl.it = https://repl.it/repls/VitalAvariciousCodec
You did not say if you are allowed to use third-party modules, and you did not say if speed is a factor. If you want to add a new item to your sorted list quickly and you are allowed to use a module, use the SortedList class from sortedcontainers. This is a module included in many distributions of Python, such as Anaconda.
This will be simple and fast, even for large lists.
someList = SortedList(["a", "b", "d"])
someList.add("c")
print(someList)
The printout from that is
SortedList(['a', 'b', 'c', 'd'])
>>> import bisect
>>> someList = ["a", "b", "d"]
>>> bisect.insort(someList,'c')
>>> someList
['a', 'b', 'c', 'd']
>>>
If standard lib is allowed you can use bisect:
>>> import bisect
>>> lst = list('abcefg')
>>> for x in 'Adh':
... lst.insert(bisect.bisect(lst, x), x)
... print(lst)
...
['A', 'a', 'b', 'c', 'e', 'f', 'g']
['A', 'a', 'b', 'c', 'd', 'e', 'f', 'g']
['A', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

Python: sets and set.intersection - random order of output?

I have a question about the un-ordered nature of sets.
This code:
#Set1 is 'a' to 'e' in alpha order
set1 = {}
set1 = {'a', 'b', 'c', 'd', 'e'}
print('\nSet1 :', set1)
#Set2 is 'f' to 'a' (missing 'e') in reverse-alpha order
set2 = {}
set2 = {'f', 'd', 'c', 'b', 'a'}
print ('Set2 :', set2)
print ('Common to both sets:', set1.intersection(set2))
...gives random ordering of the elements in set1, set2 and in the result of set.intersection:
Set : {'a', 'c', 'b', 'e', 'd'}
Set: {'a', 'c', 'b', 'd', 'f'}
Common to both sets: {'a', 'c', 'b', 'd'}
Although not a problem per se, my question is this: is there a set algorithm for this? Or could I (feasibly) use this property to generate random lists of items present in two lists (i.e. is it truly random?). BTW, I have no idea why I might want to do this - thinking out loud.
The order in which sets are printed is based on, among other things, a hash of their contents - it is not random. If you need a set to be ordered, you can always use the built-in sorted() function:
>>>> sorted(set1.intersection(set2))
{'a', 'b', 'c', 'd'}

Custom Python Sort: Double Precedence

I want to make a custom sort for my Python code that uses the built in sort() function, but can sort a list based on two values. The list I want sorted is structured as a list of tuples, which each contain 2 integers. What I want to sort to do is sort the list of tuples based on each of their first integers, but if two first integers are tied, it refers to their second integer, which is unique and therefore will not be the same. I want to use the speed of the built in sort() function, but be able to sort in this way. Any and all help is GREATLY APPRECIATED!
Built in sorted does this.
>>> l = [(1, 1), (1, 2), (2, 5), (2, 4)]
>>> sorted(l)
[(1, 1), (1, 2), (2, 4), (2, 5)]
The difference between sort() and sorted() is that sort() modifies the given list (and therefore, any other lists that are sharing its structure), while sorted() accepts an iterable, and returns a brand new list object.
For instance:
>>> a = list("alphabet")
>>> a
['a', 'l', 'p', 'h', 'a', 'b', 'e', 't']
>>> b = a
>>> b
['a', 'l', 'p', 'h', 'a', 'b', 'e', 't']
>>> b.sort()
>>> #this has modified the shared structure
>>> a
['a', 'a', 'b', 'e', 'h', 'l', 'p', 't']
As opposed to sorted()
>>> c = list("alphabet")
>>> d = c
>>> sorted(d)
['a', 'a', 'b', 'e', 'h', 'l', 'p', 't']
>>> c
['a', 'l', 'p', 'h', 'a', 'b', 'e', 't']
sorted() is safer.
You have just described the exact behavior of the list.sort() method, so once you have the tuples in a list, simply call the list's sort method (l.sort) with no arguments and it will be put in the desired order.
When more complex sorts are required you can pass a "key function" as a named argument, key. The function is applied to each element of the list to generate a sort key, then the elements are sorted in the order of their sort keys.
The sorted built-in function is convenient when you require a sorted copy of a list - it simply saves you the trouble of creating a copy then calling its sort method.

Categories

Resources