简体   繁体   中英

Python unpacking list value in kwargs

I have list of dicts such as this.

rows = [
{'user': staff_user, 'grade': [u'0.0', u'N/A', u'N/A', u'N/A', u'N/A']},
{'user': non_staff_user, 'grade': [u'0.0', u'N/A', u'N/A', u'N/A', u'N/A']},
]
for row in rows:    
    expected_row = get_expected_rows(**row)



  def get_expected_rows(user, grade):
     #Here i want to list `grade` to be unpacked.
    dict(zip(
        ['username', 'email', 'total', 'sub1grade', 'sub2grade', sub3grade, sub4grade],
        user.username,
        user.email,
        grade #I want this unpacked
    ))

How can i unpack grade in the method? Ideas?

You are doing it wrongly. For creating a dictionary after zipping , you need to provide two lists to zip, first list being the list of keys and the second list being the list of values. You have already created the list of keys, now you need to create a list of values.

This is because dict() function requires an iterable that yields tuples of two elements (or a mapping of elements, or another dictionary), where the first element is the key and the second element is the value. So this can be achieved by zipping two lists one of keys and the other one of values. When zipping, zip() would take the element from corresponding indexes and produce tuples of two elements.

Example -

def get_expected_rows(user, grade):
    #Here i want to list `grade` to be unpacked.
    return dict(zip(
        ['username', 'email', 'total', 'sub1grade', 'sub2grade', sub3grade, sub4grade],
        [ user.username, user.email ] + grade))

Demo -

>>> rows = [
... {'user': 'staff_user', 'grade': [u'0.0', u'N/A', u'N/A', u'N/A', u'N/A']},
... {'user': 'non_staff_user', 'grade': [u'0.0', u'N/A', u'N/A', u'N/A', u'N/A']}]
>>> def get_expected_rows(user, grade):
...     #Here i want to list `grade` to be unpacked.
...     return dict(zip(
...         ['username', 'total', 'sub1grade', 'sub2grade', 'sub3grade', 'sub4grade'],
...         [user] + grade
...     ))
...
>>>
>>> for row in rows:
...     expected_row = get_expected_rows(**row)
...     print(expected_row)
...
{'sub1grade': 'N/A', 'sub3grade': 'N/A', 'sub4grade': 'N/A', 'sub2grade': 'N/A', 'username': 'staff_user', 'total': '0.0'}
{'sub1grade': 'N/A', 'sub3grade': 'N/A', 'sub4grade': 'N/A', 'sub2grade': 'N/A', 'username': 'non_staff_user', 'total': '0.0'}

Please do note, another potential issue in your code would be , that in line -

expected_row = get_expected_rows(**row)

You are overwriting expected_row in every iteration, so at the end of the iteration, it would only be the last row 's expected row. If you want to create a list of expected rows, you should do -

expected_rows = []
for row in rows:    
    expected_rows.append(get_expected_rows(**row))

Or the list comprehension alternative -

expected_rows = [get_expected_rows(**row) for row in rows]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM