0

This is my code:

chile_ranks = {'ghost': 1, 'habanero': 2, 'cayenne': 3}
rank_dict = {rank: name for name, rank in chile_ranks.items()}
chile_len_set = {len(name) for name in rank_dict.values()}
print(rank_dict)
print(chile_len_set)

Output:

{1: 'ghost', 2: 'habanero', 3: 'cayenne'}
set([8, 5, 7])

I wanted to print the length of item values respectively as they are arranged in the dictionary, but they are appended to the set in an arbitrary manner. It should be like:

set([5, 8, 7])
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
Piyush Sinha
  • 111
  • 6
  • `set`s aren't ordered. but you can use [this recipe](http://code.activestate.com/recipes/576694/) for an `OrderedSet`. – martineau Jul 31 '15 at 23:48
  • Native Python dictionaries aren't ordered either, so wanting to arrange the length values in the same order as they are in the dictionary makes little sense. You could use [`collections.OrderedDict`](https://docs.python.org/3/library/collections.html#collections.OrderedDict) to create some that are, however. – martineau Jul 31 '15 at 23:58

2 Answers2

1

Sets have no defined order. This is impossible without writing your own print function, which for example could use sorted.

A set object is an unordered collection of distinct hashable objects.
https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset

If you want to maintain the order, you might want to use normal lists. Uniqueness will need to be assured in some other way, for example checking your list before updating it.

Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65
0

There's no reason to make a set here unless you want to throw away duplicates (which sets do), which would result in no recorded value for something like 'spicy' (same length as 'ghost'). If you want to print the lengths, I would recommend doing so directly from the original dictionary. However, dictionaries are arbitrarily-ordered just like sets are, so you'll have to sort it:

>>> chile_ranks = {'ghost': 1, 'habanero': 2, 'cayenne': 3}
>>> rank_dict = {rank: name for name, rank in chile_ranks.items()}
>>> chile_len_set = {len(name) for name in rank_dict.values()}
>>> print(rank_dict)
{1: 'ghost', 2: 'habanero', 3: 'cayenne'}
>>> print(chile_len_set)
{8, 5, 7}
>>> print(*(len(rank_dict[key]) for key in sorted(rank_dict)))
5 8 7
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • I got it.The desired output can be also by attained mapping it to a list `chile_len_list = [len(name) for name in rank_dict.values()]` – Piyush Sinha Aug 01 '15 at 19:49
  • @PiyushSinha - The only difference between that and your non-working solution is that it outputs to a `list` instead of a `set`. Creating a sequence from an unordered collection produces an ordering just as arbitrary as creating an unordered collection from an unordered collection. That's why I used `sorted()`. Without `sorted()` (or some other ordering algorithm), your results will still be unstable. – TigerhawkT3 Aug 01 '15 at 20:16
  • and how does this `sorted()` method works?how does it preserve the order? – Piyush Sinha Aug 01 '15 at 22:48
  • Which vesion of python did you use? – Piyush Sinha Aug 01 '15 at 22:58
  • Dictionaries and `set`s are arbitrarily-ordered in any version of Python. When you create one, there is no order to preserve. The `sorted` function _creates_ order. Once again, _you are not working with ordered sequences_. – TigerhawkT3 Aug 01 '15 at 23:06
  • I asked the version because the dereference operator * which you used in above script is not working in my python shell. – Piyush Sinha Aug 01 '15 at 23:18