A
A
AlmazKayum2018-02-09 20:12:10
Python
AlmazKayum, 2018-02-09 20:12:10

How to replace elements of a Python list?

Script

data = {(1, 'USD_RUB'), (2, 'EUR_RUB')}
def exmo_func():
    for pair in data:
        pair[1].replace('RUB', 'RUR')
    print(data)

outputs the same sequence as before the loop.
I'm assuming this is because the replace method modifies the slice, not the element.
I'm new to python, please help.
How would you change the script to replace all RUB in the data list with RUR?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2018-02-09
@AlmazKayum

data = {(i[0], i[1].replace('RUB', 'RUR')) for i in data}

S
Stanislav Pugachev, 2018-02-09
@Stqs

AlmazKayum ,
strings are immutable data type

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question