1

I'm making a function to modify the elements in a list, but it doesn't change all the way through... My function is:

def modifyValues(l):
    for x in l:
        if x == 1:
            l[x] = 'a'
        elif x == 2:
            l[x] = 'b'
        elif x == 3:
            l[x] = 'c'
    print (l)

when

modifyValues([1, 2, 3, 2, 3, 1, 2, 2])

the output is:

['a', 'b', 'c', 2, 3, 1, 2, 2]

Why doesn't it change every value?

4 Answers 4

2

When you iterate through the loop, you are iterating the loop elements and not the indexes. You need to use enumerate to get the indexes along with the values.

A small demo can be

def modifyValues(l):
    for i,x in enumerate(l):   # Use enumerate here. 
        if x == 1:
            l[i] = 'a'
        elif x == 2:
            l[i] = 'b'
        elif x == 3:
            l[i] = 'c'
    print (l)

Output

['a', 'b', 'c', 'b', 'c', 'a', 'b', 'b']
Sign up to request clarification or add additional context in comments.

2 Comments

There's nothing Python 3 specific about the question. There was no need to add the tag.
Removed it. I was just experimenting with how the question bounces to the front page. I would have removed it on Sunday night anyway. Sorry if it caused trouble!
1

instead of using ""for x in (l) use for x in range (len(l))"" as x in (l) uses the values, not indices(indexing). Here's how you can do it:

def modifyValues(l):
for x in range(len(l)): 
    if l[x] == 1:
        l[x] = 'a'
    elif l[x] == 2:
        l[x] = 'b'
    elif l[x] == 3:
        l[x] = 'c'
print(l)

modifyValues([1, 2, 3, 2, 3, 1, 2, 2])

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

You should dictionary instead to change item in the list.

>>> def modifyValues(l):
...     d = {'a': 1, 'b': 2, 'c': 3}
...     modifyl = [k for i in l for k in d  if d[k] == i]
...     print(modifyl)
... 
>>> modifyValues([1, 2, 3, 2, 3, 1, 2, 2])
['a', 'b', 'c', 'b', 'c', 'a', 'b', 'b']
>>>

You can also use the ascii_lowercase constant from string

>>> from string import ascii_lowercase
>>> def modifyValues(l):
...     modifyl = [v for i in l for k, v in enumerate(ascii_lowercase, 1)  if i == k]
...     print(modifyl)
... 
>>> modifyValues([1, 2, 3, 2, 3, 1, 2, 2])
['a', 'b', 'c', 'b', 'c', 'a', 'b', 'b']

Comments

-2

Your code is incorrect, because when you iterate your list def modifyValues(l):

for x in l: // the value of x will be the position of value
        if x == 1:// your if condition does not check for the value in the list, it only checks the position.
            l[x] = 'a'
        elif x == 2:
            l[x] = 'b'
        elif x == 3:
            l[x] = 'c'
    print (l)

To improve your code use this as your if condition

 if l[x] == 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.