Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple python list vs dictionary

Tags:

python

So I am very new to python and I cant for the life of me figure out why these two statements evaluate differently,

[3*x for x in range(1,11) if x > 5]

[18, 21, 24, 27, 30]

{3*x for x in range(1,11) if x > 5}

set([24, 18, 27, 21, 30])

The top one makes perfect sense to me but why does the second print things in such a weird order? I know that hard brackets '[' are for lists and '{' for dictionaries.

like image 916
Karaja Avatar asked Nov 24 '25 14:11

Karaja


1 Answers

The second one is not a dictionary but a set. Both sets and dictionaries are unordered. The elements are not stored or displayed with any particular meaningful order.

like image 130
BrenBarn Avatar answered Nov 27 '25 03:11

BrenBarn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!