-2

I have the following list and dict :

[u'customer_id', u'bank_statement', u'pay_stub']

and

REQUEST_DOCUMENT_TYPE_CHOICES = (
    ('void_cheque',         _('Void Cheque')),
    ('pay_stub',            _('Pay Stub')),
    ('bank_statement',      _('Bank Statement (31 days)')),
    ('bank_statement_60',   _('Bank Statement (60 days)')),
    ('csst_statement',      _('CSST Statement')),
    ('saaq_statement',      _('SAAQ Statement')),
    ('cara_statement',      _('CARA Statement')),
    ('insurance_letter',    _('Insurance Letter')),
    ('t4',                  _('T4')),
    ('welfare_chart',       _('Welfare Chart')),
    ('raqp_chart',          _('RAQP Chart')),
    ('customer_id',         _('Customer ID')),
    ('proof_of_residence',  _('Proof Of Residence')),
    ('bankruptcy_proof',    _('Bankruptcy Proof')),
    ('consumer_proposal',   _('Consumer Proposal')),
    ('signed_contract',     _('Signed Contract')),
)

I already know I could access each element on that way

list = dict(Meta.REQUEST_DOCUMENT_TYPE_CHOICES)
list['void_cheque']

The purpose of this question is to convert the first list into

['Void Cheque', 'Bank Statement (31 days)', 'Pay Stub']

How, with a short line, could I map the first list into that last list in using the dictionnary? I know I could do it with simple for statement, but I want to code it under a single line in such a way I could return it inside a function... return your_code

David
  • 149
  • 1
  • 3
  • 14
  • What is `Meta`? Is this `Django`? If so, add the tag. The 2nd list is not a list if enclosed with `( )`, but rather a `tuple` of `tuple`s. Are you asking how to get map the `dict` values to the 1st lists element strings? – pstatix Oct 05 '17 at 16:58
  • Efficiency in terms of time complexity? Have you tried just mapping over the dictionary? – Carcigenicate Oct 05 '17 at 16:59
  • @pstatix I will not show you my `Meta` class, because it is irrelevant here. Yes, it is Django. – David Oct 05 '17 at 17:02

2 Answers2

2

Not a 1-liner for initialization, but you can use a simple list comprehension to get it:

things = [u'customer_id', u'bank_statement', u'pay_stub']

types = dict(Meta.REQUEST_DOCUMENT_TYPE_CHOICES) # dont use `list` as variable name

new_things = [types[thing] for thing in things if thing in types]
pstatix
  • 3,611
  • 4
  • 18
  • 40
  • @J.Doe You need to step through your code and see what the `dict()` method returns on your classes data when called. The answer lies there. You asked for a mapping of list elements; both @ Daniel and I have supplied that for you. – pstatix Oct 05 '17 at 17:36
2

You can use map:

choices = [u'customer_id', u'bank_statement', u'pay_stub']
choices = list(map(dict(Meta.REQUEST_DOCUMENT_TYPE_CHOICES).get, choices))
Daniel
  • 42,087
  • 4
  • 55
  • 81
  • I got `[, , ]`. How could I change `` into `Void cheque`? – David Oct 05 '17 at 17:12
  • Very clean. Comparable to list comprehension as shown [here](https://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map). I'd say it is not as _pythontic_ though. – pstatix Oct 05 '17 at 17:15
  • @J.Doe That is going to depend on how your `Meta` allows for its `REQUEST_DOCUMENT_TYPE_CHOICES` to be built into the `dict()` call. So not it appears that your class _is_ relevant. With a proper `dict` object, the code works. – pstatix Oct 05 '17 at 17:19
  • I know that I could access `Void cheque` with `.capitalized()`. Could you modify that answer so that it work with it and I will accept your answer. – David Oct 05 '17 at 17:19