Dictionary to Python data conversion

Below is the code which is wrote for dictionary to list data conversion

animal = {1: 'Lion', 2: 11, 5: 'abc', '7': 'Speed', '1': 'Tiger'}
print(animal)
print(type(animal))
print(list(animal))

The output is as follows

{1: 'Lion', 2: 11, 5: 'abc', '7': 'Speed', '1': 'Tiger'}
<class 'dict'>
[1, 2, 5, '7', '1']

Can anyone explain me why type conversion from dictionary to list ends up with index values of dictionary as elements of lists ?

That’s the expected output. See the dict class documentation:

list(d)
  Return a list of all the keys used in the dictionary d.

Use list(d.values()) to get a list of all the values in the dictionary.

Below is the code which is wrote for dictionary to list data conversion

animal = {1: 'Lion', 2: 11, 5: 'abc', '7': 'Speed', '1': 'Tiger'}
print(animal)
print(type(animal))
print(list(animal))

The output is as follows

{1: 'Lion', 2: 11, 5: 'abc', '7': 'Speed', '1': 'Tiger'}
<class 'dict'>
[1, 2, 5, '7', '1']

Can anyone explain me why type conversion from dictionary to list ends
up with index values of dictionary as elements of lists ?

Because none of these is a type conversion.

First up, remember what print() does: it takes each of its arguments and
calls str() on it to obtain a string, and writes that string to the
output. Various objects have their own idiosyncratic implementation for
what str() does (via their internal str method). The intent is that
str should return a human friendly text repesentation of the object,
not necessarily some Python expression.

Let’s take each line above in turn:

print(animal)

animal is a dict, and dict.str() actually tries to return a Python
expression describing the dict by calling repr() on its keys and values,
and sticking them together in a dictionary comprehension. So for a dict
with basic types like str and int you get this string result:

{1: 'Lion', 2: 11, 5: 'abc', '7': 'Speed', '1': 'Tiger'}

That’s text! It just looks like what you could write in a Python
programme to construct an identical dictionary. Then print() writes
that text to the output.

print(type(animal))

type() of an object returns its type or class (they are the same thing).
So this expression returns the “dict” class. str() of the dict class
returns this string:

<class 'dict'>

Most classes return a string like that: the string "<class ", the repr()
of the class name, then “>”.

print(list(animal))

This is a good one. There’s a lot going on here.

“list” is a class. You’re calling “list(animal)”, which constructs a
list from its arguments (just like you might construct a new instance of
any class, if you’ve got up to classes yet).

When list() is called, it makes the new list by iterating over the
argument you hand it. This is described in detail here:

https://docs.python.org/3/library/stdtypes.html#list

but in short: it iterates over the argument and makes a list containing
what that iteration found. An object might yield anything when you
iterate over it (of course, it should yield something reasonable, not
nonsense). For a dict, iteration yields the keys of the dict. As of
Python 3.7, those keys come out in insertion order (from when you made
the dict). So iterating over animal would yield these values:

1
2
5
'7'
'1'

and list() takes those and makes a list:

[1, 2, 5, '7', '1']

So that list object is passed to print(). print() calls str() on the
list. Like dict, list tries to return a Pythonlike expression
describing the list by calling repr() on each member of the list and
joining them together. For the list above this produces this string:

[1, 2, 5, '7', '1']

which print() then writes to the output.

There’s no “type conversion” going on. You’re just (implicitly) calling
a lot of functions and copying the result out as text.

To show a different example with list, consider that strings are
iterable, yielding each character in the string (as a single character
string - there is no separate “character” class in Python). So:

>>> list('abc')
['a', 'b', 'c']

because iterating over ‘abc’ yielded these values:

'a'
'b'
'c'

Finally, someone is bound to complain that really some type conversion
is happening. I do not like this term, or its friend “cast” which you
might see sometimes.

To me, we’re just calling various functions or methods with arguments,
and getting results. When the function takes a single argument (a list
or a dict or whatever) and returns something else, it might be doing
what you would call type conversion. For example:

>>> int("123")
123

where we poass the string “123” to int() and get back in integer value.
You could describe this as “converting” a string to an integer. To me,
it is just a function call which happens to do what you would expect
“conversion” to do. Conversely:

>>> str(123)
'123'

taking an integer and returning a string. But we could equaly have some
“square()” function which returned 15129. Is that conversion? Surely
not.

The idiom might be if you call some_class_name(other_value) to get an
instance of some_class_name, it should be “conversion”. int(“123”) and
str(123) fit that pattern. But there are many counter examples which
have the same shape.

Your list(animal) above is such a counter example: it is not
“converting” a dict to a list - that might imply you could go back from
that list to the original dict. But you can’t - it just makes a shiny
new list from the keys of the dict.

This is why I’m not a fan of the term “type conversion”. It’s a
meaningful idea if you can undo it. But otherwise I think it a little
misleading.

Cheers,
Cameron Simpson cs@cskk.id.au