Converts sequence of (Key, Value) pairs to a dictionary. >>> type(seq([('a', 1)]).to_dict()) dict >>> seq([('a', 1), ('b', 2)]).to_dict() {'a': 1, 'b': 2} :param default: Can be a callable zero argument function. When not None, the returned
(self, default=None)
| 1427 | return self.to_set() |
| 1428 | |
| 1429 | def to_dict(self, default=None): |
| 1430 | """ |
| 1431 | Converts sequence of (Key, Value) pairs to a dictionary. |
| 1432 | |
| 1433 | >>> type(seq([('a', 1)]).to_dict()) |
| 1434 | dict |
| 1435 | |
| 1436 | >>> seq([('a', 1), ('b', 2)]).to_dict() |
| 1437 | {'a': 1, 'b': 2} |
| 1438 | |
| 1439 | :param default: Can be a callable zero argument function. When not None, the returned |
| 1440 | dictionary is a collections.defaultdict with default as value for missing keys. If the |
| 1441 | value is not callable, then a zero argument lambda function is created returning the |
| 1442 | value and used for collections.defaultdict |
| 1443 | :return: dictionary from sequence of (Key, Value) elements |
| 1444 | """ |
| 1445 | dictionary = {} |
| 1446 | for e in self.sequence: |
| 1447 | dictionary[e[0]] = e[1] |
| 1448 | if default is None: |
| 1449 | return dictionary |
| 1450 | else: |
| 1451 | if hasattr(default, "__call__"): |
| 1452 | return collections.defaultdict(default, dictionary) |
| 1453 | else: |
| 1454 | return collections.defaultdict(lambda: default, dictionary) |
| 1455 | |
| 1456 | def dict(self, default=None): |
| 1457 | """ |
no outgoing calls