Skip to content
Advertisement

append only if item isn’t already appended?

In my Python application, I have the following lines:

for index, codec in enumerate(codecs):
    for audio in filter(lambda x: x['hls']['codec_name'] == codec, job['audio']):
        audio['hls']['group_id'].append(index)

How can I only trigger the append statement if the index hasn’t been already appended previously?

Advertisement

Answer

Simply test if your index not in your list:

for index, codec in enumerate(codecs):
    for audio in filter(lambda x: x['hls']['codec_name'] == codec, job['audio']):
        if index not in audio['hls']['group_id']:
            audio['hls']['group_id'].append(index)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement