>>>listoflist s = [['a', 'first'], ['b', 'second'], ['a', 'next'], ['c', 'third'], ['b', 'last']]
>>> dd = {}
>>>for item in listoflists:
... dd.setdefault(i tem[0], []).append(item)
- The part which mentions 'item[0]' would be to have the first letter of each item in the list we created right ?
- what does the ' [ ] ' signify in this case ? Does it mean to have a null value...
User Profile
Collapse
-
Python:Query on Conditional Loops and the setdefault method
-
Hey Thanks a ton !!!
Your answer's been very helpful.
A couple of clarifications that you could perhaps help me with:
You mentioned:
>>>for item in listoflists:
... dd.setdefault(i tem[0], []).append(item[1])
- The part which mentions 'item[0]' would be to have the first letter of each item in the list we created right ?
- what's the ' [ ] ' signify in this case... -
A problem that I am facing on Python Dictionaries
I input the following:
>>> collection={} #Initiate an empty dictionary
>>>collection={ 'a':'first', 'a':'second','d ':'third','b':' fourth','e':'fi fth','b':'sixth '}
>>> collection
{'a':'second',' b':'sixth','e': 'fifth','d':'th ird'}
Now my question is:
Why does the output not include all the values (2 values for 'a' AND 2 values for 'b) BUT only shows the output of ONLY the...
No activity results to display
Show More
Leave a comment: