How to access the elements of a set (set)?

Asked

Viewed 1,283 times

8

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

I created two set examples using two distinct 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 indexing 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

Therefore, I would like to know how I could access the elements of a set individually?

2 answers

8

The set has as its purpose define a single list of elements.

One was able to do that would be convert to list and get the value through the index.

Thus:

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

print(list(a)[0])

UPDATING

the set Python is a collection of disordered elements and, according to a response I read in SOEN, it makes no 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 is either in a set or it is not. This means that no element in a set has an index.

That means that in a set (set), the goal is only to gather 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 for a list and 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 I developed, I used set to add the connections that were made on the server in a unique way. The order in my case didn’t matter, but just the fact that it was a collection of unique records.

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

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

  • 3

    "For some reason" > The reason is that elements of set do not have "order" so they cannot be indexed in a way that makes sense. When converting to list with list(a) as you did, there’s no way to guarantee that the element 1 be the first on the list, it may fall into another position. One way is to use sorted(a) which ensures that the elements will be in numerical order.

  • I still haven’t had to add more reference, I’m stuck here, kkkkk.

  • In these answers from Soen there are some ways to keep order. In the first example, with an entry [2, 60, 19, 51, 'stack', 'py', 60, 'stack'] the exit is [2, 60, 19, 51, 'stack', 'py']

8


The data structure set of Python has no ordering - so it would make no sense to say "me give the element at position 0" (meuset[0]) - however, it is eternal!

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

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

That’s enough, and the most correct form for most cases. If you really need to do several operations on arbitrary set elements, you can put these elements in a list - as is an iterable:

minha_lista = list(meu_set)

It works - but the list won’t be in any specific order - (so why not use the for above). If you need some order, you can use the built-in sorted which 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 - suppose you want a list of elements in your set in ascending length order:

minha_lista = sorted(meu_set, key=len) 

This will use the function return len in each item to make the ordering. Details osbre the Sorted is the elements of the set are compared to each other, so it wouldn’t work for sets with mixed object types from your examples (it might work if you write a function key that works for the various types of objects).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.