How to access the elements of a set?

5

I'm studying about sets ( set ) in Python because I'm going to use an algorithm to find paths in a graph, it's for a college discipline I'm having.

I created two assembly examples using two different notations. Look at the illustration example:

foo = {1, 1, 5, 'gato', 'Oie'}
baz = set([2, 19, 51, 'stack', 'py'])

print(foo)
print(baz)

However, when I try to access some value of the two sets foo or baz using index baz[0] it returns an error saying that the object does not support indexing, see:

Traceback (most recent call last):
  File "jdoodle.py", line 11, in <module>
    print(baz[2])
TypeError: 'set' object does not support indexing
Command exited with non-zero status 1

Question

So, I'd like to know how I could access the elements of a set individually?

    
asked by anonymous 06.09.2018 / 20:59

2 answers

6

The data structure set of Python has no sort order - so it would not make sense to say "give me the element in position 0" (meuet [0]) - however it is iterable!

So if you have an action to perform with each element of a set, you can simply do:

for element in my_set:
    # do things with 'element'

That's enough, and the right way for most cases. If you really need to do multiple operations on arbitrary elements of the set, you can put those elements in a list - as it is an iterable:

minha_lista = list(meu_set)

It works - but the list will not be in any specific order - (so why not use for above). If you need some order, you can use the built-in sorted that returns a list, but sorts the elements - so if you want the elements in alphabetical order:

 minha_lista = sorted(meu_set)  

Or, if you want an arbitrary order, you can use the parameter key for sorted - let's suppose you want a list of elements in your set in ascending order of length:

minha_lista = sorted(meu_set, key=len) 

This will use the return of the len function on each item to sort. Details about the sorted is that elements of set are compared to each other, so it would not work for sets with mixed object types of their examples (it might work if you write a key function that works for the various object types ).

    
06.09.2018 / 22:11
5

set is intended to define a single list of elements .

One were able to do this would be to convert to list and get the value through the index.

So:

a = set([1, 2, 3, 4])

print(list(a)[0])

UPDATE

set of Python is a cluttered element collection, and according to a response I read in SOEN , it does not make sense to try to access it by an index.

See the translation

  

A set is just a cluttered collection of unique elements. So an element either is in a set or is not. This means that no element in a set has an index.

This means that in a set ( set ), the goal is only to collect certain elements in a collection, in a unique way. The position in which the element is allocated matters little, since this is not the goal of set .

What can be done in such cases is to convert set to list and to propose some logical order for its structure.

I think in your case, the idea is to use the right kind of structure for your data.

For example, on a websocket server that I developed, I used set to add the connections that were made to the server in a unique way. The order in my case did not matter, but just the fact that it was a collection of unique records.

If I needed to use it to find out who entered first, I could use a list . If I wanted to name each connection by a unique ID, I would use dict .

That is, each structure should be used for the correct case.

    
06.09.2018 / 21:05