SeesSound
SeesSound

Reputation: 505

python replacing item in list

Hi my question relates to a post a long time ago by some user here:

Find and replace string values in Python list

For my specific situation. I have a list like this:

appl =  ['1', 'a', 'a', '2', 'a']

I want to replace only the "a single unknown" instance of "a" with just an empty space(that holds the position of where "a" used to be). I am unsure of how to do it for only one character. Can anyone help? Thanks in advance.

EDIT: i Should mention that I need to use an "index" function to first locate the "a" since it is a changing variable. And then I need code that will replace the character.

EDIT2: A lot of people are assuming the index will be 2, but I want to point out that the index of the character is not known. Also "a" will be present in the list approximately ~20 times(will remain a fixed number) but their locations will be changing. I want to replace an "a" based on user input. This is the actual list I am working with:

track  [2] =   ["|","@","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"]

@ is a charachter, | is a border and " " is an empty space. A user enters input to choose how much @ moves to the right and a new image is displayed that shows the new location of @ and the old location of @ is replaced with a space. The length of the list remains constant. This is the context of my question.

Upvotes: 5

Views: 32892

Answers (5)

jfs
jfs

Reputation: 414139

It seems your list (with "|", "@") is short and "@" is always present, just in different locations. In this case you can replace the first occurrence of "@" in the appl list in one line:

appl[appl.index("@")] = " "

If the list is large and the item might not be present:

i = next((i for i, x in enumerate(lst) if x == item), None)
if i is not None:
   lst[i] = replacement

Upvotes: 0

Serdalis
Serdalis

Reputation: 10489

You may use index to achieve this goal, along with a loop. Here is a little function for it:

def replace_element(li, ch, num, repl):
    last_found = 0
    for _ in range(num):
        last_found = li.index(ch, last_found+1)
    li[li.index(ch, last_found)] = repl

use:

replace_element(appl, 'a', 2, ' ')

This method is particularly nice because it throws the following error when the charactor is not in the list:

ValueError: 'a' is not in list

Upvotes: 2

jterrace
jterrace

Reputation: 67063

First, find the first occurrence of the value:

>>> appl = ['1', 'a', 'a', '2', 'a']
>>> first = appl.index('a')
>>> first
1

To find the second, start at the first location plus one:

>>> second = appl.index('a', first+1)
>>> second
2

Then set that location to a space like you want:

>>> appl[second] = ' '
>>> appl
['1', 'a', ' ', '2', 'a']

Upvotes: 1

phihag
phihag

Reputation: 287775

You can simply find the index of the second occurrence by traversing the list and storing the indexes whose elements are 'a' until you've found two:

>>> appl = ['1', 'a', 'a', '2', 'a']
>>> idxs = (i for i,c in enumerate(appl) if c == 'a')
>>> next(idxs)
0
>>> appl[next(idxs)] = ''
>>> appl
['1', 'a', '', '2', 'a']

Upvotes: 3

Eric
Eric

Reputation: 97575

appl = ['1', 'a', 'a', '2', 'a']
a_indices = [i for i, x in enumerate(appl) if x == 'a']

if len(a_indices) > 1:
    appl[a_indices[1]] = ' '

Upvotes: 1

Related Questions