Python: how to set value to a list element -


this question has answer here:

i have list, named mac, saves 6-bytes mac address. want set last byte 0, use:

mac[5] = 0 

but gives me error:

typeerror: 'str' object not support item assignment 

how fix error?

because mac str , strings immutable in python.

mac = list(mac) mac[5] = '0' mac = ''.join(mac) #to mac in str 

or use bytearray, function mutable string.

>>> x = bytearray('abcdef') >>> x bytearray(b'abcdef') >>> x[5] = '0' >>> x bytearray(b'abcde0') >>> str(x) 'abcde0' 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -