Renier
Renier

Reputation: 1535

how to get value from a list of tuples and make a new list

I have the following:

[('first_name', u'Renier'), ('second_name', u''), ('surname', u'de Bruyn'), ('own_transport', u'True'), ('id_number', u'910920254081'), ('id_type', u'IDNumber'), ('id_nationality', u'ZA'), ('disabilities_list', u'Intellectual Disability'), ('disabilities_list', u'Memory Loss'), ('disabilities_list', u'Mental Illness'), ('disabilities_list', u'Physical Disability'), ('disabilities_list', u'Speech and Language Disorders'),...]

How would I get the values of these tuples and use, where there is a value disabilities_list, to create a new list?

I have tried:

 for k in arr:
        if k == "disabilities_list":
            print k # this only returns "disabilities_list" 5 times

Update:

arr is what is returned from a form, this is waht I get when I print arr:

NestedMultiDict([('first_name', u'Renier'), ('second_name', u''), ('surname', u'de Bruyn'), ('race', u'Caucation'), ('own_transport', u'True'), ('id_number', u'9109205021082'), ('id_type', u'IDNumber'), ('id_nationality', u'ZA'), ('disabitity', u'True'), ('disabilities_list[]', u'Memory Loss'), ('disabilities_list[]', u'Mental Illness'), ('disabilities_list[]', u'Physical Disability'), ('disabilities_list[]', u'Speech and Language Disorders'), ('disabilities_list[]', u'Vision Loss and Blindness'), ('mobile_number', u'0828245092'), ('home_number', u'0317016554'), ('email', u'[email protected]'), ('other_contact', u''), ('line1', u'2 Hamilton Road'), ('line2', u''), ('line3', u''), ('city_town', u'Durban'), ('province', u'GP'), ('postal_code', u'3610'), ('suburb', u'Ashley'), ('country', u'ZA'), ('notice_period', u'4 weeks'), ('for_position', u'Developer'), ('curr_renumeration', u'12.0'), ('expect_renumeration', u'123.0'), ('relocate', u'True')])

Upvotes: 2

Views: 134

Answers (3)

Alexander Zhukov
Alexander Zhukov

Reputation: 4547

Use a list comprehantion:

>>>arr = [('first_name', u'Renier'), ('second_name', u''), ('surname', u'de Bruyn'), ('own_transport', u'True'), ('id_number', u'910920254081'), ('id_type', u'IDNumber'), ('id_nationality', u'ZA'), ('disabilities_list', u'Intellectual Disability'), ('disabilities_list', u'Memory Loss'), ('disabilities_list', u'Mental Illness'), ('disabilities_list', u'Physical Disability'), ('disabilities_list', u'Speech and Language Disorders'),...]
>>>
>>>[k[1] for k in arr if k[0] == "disabilities_list[]"] 
[u'Intellectual Disability', u'Memory Loss', ...]

edit: updated, according to the updated question:

>>>[item[1] for item in arr.items() if item[0] == "disabilities_list"]  #  will do, what you want

Upvotes: 4

vahid abdi
vahid abdi

Reputation: 10278

Maybe this is what you want:

>>> for k,v in arr.iteritems():
...     if k == 'disabilities_list':
...         print v

Upvotes: 1

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

if k[0] == "disabilities_list":

Upvotes: 1

Related Questions