Random guy
Random guy

Reputation: 923

Iterating through list in python throwing error

I am new to python3. I have some requirement like this:

function : predictRisk
Parameters :
1 :
positional / keyword
type dicitionary
Note : return value of function prepareData
2 :
Risk Zones
Type : lists
returns riskiness of that person based on the places s/he has visited. If a person has visited a place which is identified to be in risk zones, then the person should be quarantined for at least 14 days.

So,I tried like this:

def predictRisk(**k):
    x = k['visited']
    print(x)
    riskzones = ['kapan', 'chabail', 'newroad']
    for i in riskzones:
        for k,v in x:
          if v == i:
            return 'You are in danger zone'
          else:
            return 'You are not in danger zone'

for 2nd parameter I need to pass list,so I tried, xyz list didnot worked here and i removed this way.

 def predictRisk(**k,xyz=[]):
        x = k['visited']
        print(x)
        riskzones = ['kapan', 'chabail', 'newroad']
        for i in riskzones:
            for k,v in x:
              if v == i:
                return 'You are in danger zone'
              else:
                return 'You are not in danger zone'

So,I tried as:

k=prepareData('arun','nepali',location1='chabail',location2='kathmandu')
print(k)
predictRisk(**k)

But,I got error as:

{'name': 'arun', 'national': 'nepali', 'visited': [{'location1': 'chabail'}, {'location2': 'kathmandu'}]}
[{'location1': 'chabail'}, {'location2': 'kathmandu'}]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-119-fabcf7206f8b> in <module>
      2 print(k)
      3 
----> 4 predictRisk(**k)

<ipython-input-117-82fada056b90> in predictRisk(**k)
      4     riskzones = ['kapan', 'chabail', 'newroad']
      5     for i in riskzones:
----> 6         for k,v in x:
      7           if v == i:
      8             return 'You are in danger zone'

ValueError: not enough values to unpack (expected 2, got 1)

Why is this error coming?I searched for the answers but couldnot found the proper answer.Is it also possible riskzones = ['kapan', 'chabail', 'newroad'] that i can pass list as argument when calling predictRisk()?

Upvotes: 0

Views: 85

Answers (3)

Red
Red

Reputation: 27547

As you can see, x equals to [{'location1': 'chabail'}, {'location2': 'kathmandu'}].

Saying for k,v in x will not retrieve the keys and values.

Try:

    for i in riskzones:
        for k,v in {k:v for d in x for k,v in d.items()}.items():
          if v == i:
            return 'You are in danger zone'
          else:
            return 'You are not in danger zone'

Upvotes: 2

Neil
Neil

Reputation: 3281

Oh dear! This is bad:

def predictRisk(**k,xyz=[]):

1 NEVER use mutable objects as parameter defaults. They maintain state between function invocations! This will get you some bugs that are impossible to locate. use this:

def predictRisk(**k, xyz=None):
    if xyz is None:
        xyz = []

2 Put args and kwargs last in the signature. Otherwise k is just going to grab everything. (also, side note, is it really that much effort to write out the word kwargs? One letter variables are... argh.

def predictRisk(xyz=None, **kwargs):

    if xyz is None:
        xyz = []

3 This is Python, snake case please!

def predict_risk(xyz=None, **kwargs):
    if xyz is None:
        xyz = []

That should already improve some things.

Upvotes: 1

Vijay Anand Pandian
Vijay Anand Pandian

Reputation: 1165

The errored because of x is not a dict type.

def predictRisk(**k):
    x = k['visited']
    riskzones = ['kapan', 'chabail', 'newroad']
    for i in riskzones:
        for v in [_.values()[0] for _ in x]:
          if v == i:
            return 'You are in danger zone'
          else:
            return 'You are not in danger zone'





k = {'name': 'arun', 'national': 'nepali', 'visited': [{'location1': 'chabail'}, {'location2': 'kathmandu'}]}

print(predictRisk(**k))

Upvotes: 0

Related Questions