Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the only item in a set containing one item

Tags:

julia

I have a set containing a single item, in this case a string:

b = Set(["A"])

I want to get that single item out. What's the best way of doing this? The only way I can figure out to do it is using a loop:

single_item = ""
for item in b
    single_item = item
end

which gets my what I need

julia> single_item
"A"

but I feel like there must be an easier way.

like image 366
Ian Marshall Avatar asked Nov 09 '16 09:11

Ian Marshall


People also ask

Is it possible to retrieve a single element from set?

You can use an Iterator to both obtain the only element as well as verify that the collection only contains one element (thereby avoiding the size() call and the unnecessary list creation): Iterator<Element> iterator = set.

How do you take one element from a list?

There are three ways in which you can Remove elements from List: Using the remove() method. Using the list object's pop() method. Using the del operator.

How do you get a specific element in a list Python?

Find an element using the list index() method. To find an element in the list, use the Python list index() method, The index() is an inbuilt Python method that searches for an item in the list and returns its index. The index() method finds the given element in the list and returns its position.


1 Answers

how about

julia> collect(b)[1]
"A"

edit

as the legendary Dan Getz suggested, consider doing

julia> collect(take(b,1))[1]
"A" 

if memory is an issue

like image 167
isebarn Avatar answered Oct 05 '22 13:10

isebarn