-2

Here is my code:

my_list=[2794, 2798, 2817, 2820, 2821, 2823, 2824, 2826, 2829, 2831, 2831, 2831, 2839, 2842, 2843, 2845, 2847, 2850, 2851, 2854, 2855, 2856, 2857, 2858, 2859, 2862, 2864, 2865]

for i in my_list:
    print(i)


print("\n\n")

my_set = set(my_list)

for i in my_set:
    print(i)

output:

2794
2798
2817
2820
2821
2823
2824
2826
2829
2831
2831
2831
2839
2842
2843
2845
2847
2850
2851
2854
2855
2856
2857
2858
2859
2862
2864
2865



2817
2820
2821
2823
2824
2826
2829
2831
2839
2842
2843
2845
2847
2850
2851
2854
2855
2856
2857
2858
2859
2862
2864
2865
2794
2798


Question: Why list (in ascending) and set (in some other) are printed in different order?

vasili111
  • 6,032
  • 10
  • 50
  • 80

2 Answers2

1

That is happening because as @user2357112supportsMonica said, sets are unordered. There is an ordered set though which you can see here.

xilpex
  • 3,097
  • 2
  • 14
  • 45
0

set is unordered and un-indexed data structure. where as list is ordered data stucture.

convert it to list

for i in list(my_set):
    print(i)
Rima
  • 1,447
  • 1
  • 6
  • 12