Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python syntax error with list filtering

Tags:

python

I'm trying to filter an item from a list, and I'm getting a syntax error: SyntaxError: invalid syntax

The code:

a['name'] = 'Dan'
b['name'] = 'Joe'

ppl = [a,b]
inputName = raw_input('Enter name:').strip()
person = [p in ppl if p['name']==inputName].pop()

any idea?

like image 270
Gil Avatar asked Jan 31 '26 07:01

Gil


2 Answers

[item for item in array] not [item in array]

like image 104
Marcus Avatar answered Feb 01 '26 21:02

Marcus


First of all, you should use dictionary instead of list if you want to use 'name' key. It should look like this

    a = {'name':'Dan'}
    b = {'name':'Joe'}
    ppl = [a,b]
    for p in ppl:
        if(p['name']==inputName):
            person=ppl.pop(ppl.index(p))

Maybe there is a better way, more pythonic, but this one working ;)

like image 34
bognix Avatar answered Feb 01 '26 19:02

bognix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!