13

Can somebody explain this code ?

l3 = [ {'from': 55, 'till': 55, 'interest': 15}, ]
l4 = list( {'from': 55, 'till': 55, 'interest': 15}, )

print l3, type(l3)
print l4, type(l4)

OUTPUT:

[{'till': 55, 'from': 55, 'interest': 15}] <type 'list'>
['till', 'from', 'interest'] <type 'list'>
2
  • I guess list uses dict.keys() and the first one is a list of dicts. Commented May 16, 2014 at 19:25
  • 4
    dict is an iterable (by keys), the list() takes an iterable and returns the list. Commented May 16, 2014 at 19:26

4 Answers 4

23

When you convert a dict object to a list, it only takes the keys.

However, if you surround it with square brackets, it keeps everything the same, it just makes it a list of dicts, with only one item in it.

>>> obj = {1: 2, 3: 4, 5: 6, 7: 8}
>>> list(obj)
[1, 3, 5, 7]
>>> [obj]
[{1: 2, 3: 4, 5: 6, 7: 8}]
>>> 

This is because, when you loop over with a for loop, it only takes the keys as well:

>>> for k in obj:
...     print k
... 
1
3
5
7
>>> 

But if you want to get the keys and the values, use .items():

>>> list(obj.items())
[(1, 2), (3, 4), (5, 6), (7, 8)]
>>> 

Using a for loop:

>>> for k, v in obj.items():
...     print k, v
... 
1 2
3 4
5 6
7 8
>>> 

However, when you type in list.__doc__, it gives you the same as [].__doc__:

>>> print list.__doc__
list() -> new list
list(sequence) -> new list initialized from sequence's items
>>> 
>>> print [].__doc__
list() -> new list
list(sequence) -> new list initialized from sequence's items
>>> 

Kind of misleading :)

Sign up to request clarification or add additional context in comments.

1 Comment

__doc__ is class(list) attribute, no wonder its instances([]) have that too.
7
  • The former just wraps the entire item in square brackets [], making it a one-item list:

    >>> [{'foo': 1, 'bar': 2}]
    [{'foo': 1, 'bar': 2}]
    
  • The latter iterates over the dictionary (getting keys) and produces a list out of them:

    >>> list({'foo': 1, 'bar': 2})
    ['foo', 'bar']
    

1 Comment

One more thing worth to be mentioned: if you want to create only one element in the list. list() might cause error such as, list(3). Only [3] can work.
3
>>> help(list)

Help on class list in module __builtin__:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items

In the first case the notation indicates you're creating a list with a dictionary as its object. The list is created empty and the dictionary is appended as an object.

In the second case you're calling the second form of the list constructor - "initialized from iterable's items". In a dictionary it's the keys that are iterable, so you get a list of the dictionary keys.

Comments

3

The list is a constructor that will take any basic sequence (so tuples, dictionaries, other lists, etc.) and can turn them into lists. Alternatively, you could make the lists with [] and it will make a list with all the things you put inside the brackets. You can actually accomplish the same things with list comprehension.

 13 = [item for item in {'from': 55, 'till': 55, 'interest': 15}]
 14 = list({'from': 55, 'till': 55, 'interest': 15})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.