0

I was trying to find a way to reverse the elements of a set. In the process of conversion we should not convert set into any other data type like list and we should not use recursions

Is their any way for it ?

Ex : S= {1,2,3}

Output:

3,2,1

  • 5
    `set` objects do not provide any public way to reverse them because *sets are inherently unordered data types*. Any particular order that it iterates in is arbitrary. Barring things like hacking the internals, you'd have to use an auxiliary, ordered data structure like a list (or implicitly the call stack, using recursion), but again, this doesn't really make sense because code you write shouldn't rely on any order about sets. – juanpa.arrivillaga May 03 '21 at 04:29
  • If you wanted to iterate over the elements in the reverse order of the order in which you inserted them, you can use a `dict` instead, as described [here](https://stackoverflow.com/a/9792680/5468953), since these preserve insertion order - so if `d` is a `dict`, you can iterate over `reversed(d)`. (Commenting as it's not clear if this is what OP was asking, but I was wondering about this use case and ended up in this question) – Izaak van Dongen Jan 03 '23 at 14:14

2 Answers2

4

Sets are not ordered. So, the sets {1, 2, 3} and {3, 2, 1} are essentially the same.
You must use lists if you want ordered elements.

list1 = [1, 2, 3]
list2 = list1[::-1]
print(list2)

Output is

>>> [3, 2, 1]

EDIT: If you want to use for loop:

list1 = [1, 2, 3]
list2 = []
for x in range(len(list1)-1, -1, -1):
    list2.append(list1[x])
    
print(list2)

The logic of this code is: The range(a, b, c) function takes 3 arguments : from a to (b-1) with each step of c.
So, we move from the last element (len(list1)-1) of the list1 upto the first element -1 with a step of -1, so that it decreases with each step.

CoolCoder
  • 786
  • 7
  • 20
  • Thanks for your reply I understand that So can't we print their in reverse order – Deekshith Ranga Babu May 03 '21 at 04:05
  • You could convert it into a list and then reverse that, which would print them in reverse order from how they print normally, although it doesn't really make much sense to use a set for this kind of case. – duckboycool May 03 '21 at 04:10
  • I know that but I want know is there any way by using for loop – Deekshith Ranga Babu May 03 '21 at 04:14
  • ya! But I want to use only set not list . Thanks for your reply. – Deekshith Ranga Babu May 03 '21 at 04:59
  • 1
    @pavankrishna, note that ***SETS ARE NOT ORDERED***. So, you can't guarantee that after performing any operation on the set, the "order" of the set remains the same. Also, you can't access the elements in a set the "normal" way, like we do in lists: `my_list[index] = allowed`, `my_set[index] = not allowed` – CoolCoder May 03 '21 at 05:05
  • I am not trying to change the order of Elements in a set. I was trying only to print them in decreasing order. – Deekshith Ranga Babu May 03 '21 at 05:10
2

I think this obeys your rules and prints the set in descending order:

s = {1, 2, 3}
for _ in range(len(s)):
    elem = max(s)
    s.remove(elem)
    print(elem, end=',' if s else '')

# 3,2,1
Jamie Deith
  • 706
  • 2
  • 4