Faslo
Faslo

Reputation: 1

Modify items in a list does not give any output

I'm finding really difficult modifying some items in a list. I have a list comparable to a database, and I want to convert string values (which I would consider as missing values) to 99.

database = [2,5,11,33,78498,'abcqwe',13, 18,11,1993,'defrty']

def missing_values(i):
    for i in database:
        if type(i) == str:
            i = 99
    return 'Database cleaned'

When I run it, there are no syntax errors, even though "Database cleaned" does not appear. If I print(database), I see that nothing has actually changed. What I'm doing wrong?

Upvotes: 0

Views: 62

Answers (4)

Ali
Ali

Reputation: 1357

The reason you don't see the list cleaned is because you don't return the list after cleaning, you instead just print a msg leaving the original list unchanged.

database = [2,5,11,33,78498,'abcqwe',13, 18,11,1993,'defrty']

def missing_values(i):
    for i in database:
        if type(database[i]) == str:
            database[i] = 99
    print 'Database cleaned'
    return database

Also you need to change the argument used in the function to from i to database. I assumed the i parameter is supposed to be the list that needs cleaning.

Upvotes: 0

Kyle Higginson
Kyle Higginson

Reputation: 942

In the function, you are setting i to 99, but never the value in your list. Where you set i = 99, you should first get the index of this value in your list, then set the value like this:

database[database.index(i)] = 99

You should also ensure you call the function as Abe said. There is also no need to pass a value through to this function.

Upvotes: 2

Abe
Abe

Reputation: 1415

You're not calling your function properly. Make sure then in somewhere of your code missing_values() it's been called.

Upvotes: 0

Luke Glazebrook
Luke Glazebrook

Reputation: 580

You need to make sure that you are calling your missing_values() function somewhere in your file otherwise the code will never be executed.

missing_values()

Also, seeing as you have defined the database variable in the same scope as missing_values you do not need the argument i.

Upvotes: 0

Related Questions