August Williams August Williams - 1 month ago 6
Python Question

How can I extend/append a list with multiple indexes from a variable?

I am trying to create a binary32 floating point to decimal converter, which is made up of eight bits for the exponent and twenty four bits for the mantissa. I have

exponent = []
and
mantissa = []
. If the user inputs
010000111111101101000000000000000
I want indexes one to eight from
value
to be added to
exponent
and indexes nine to thirty two from
value
to be added onto
mantissa
. I currently have the following spaghetti code to do this:

print ("Welcome to August's floating point value to decimal converter!")
value = input("Please enter 32 bit floating value to convert.")
exponent = []
mantissa = []
exponent.append(value[1])
exponent.append(value[2])
exponent.append(value[3])
exponent.append(value[4])
exponent.append(value[5])
exponent.append(value[6])
exponent.append(value[7])
exponent.append(value[8])
print (exponent)
mantissa.append(value[9])
mantissa.append(value[10])
mantissa.append(value[11])
mantissa.append(value[12])
mantissa.append(value[13])
mantissa.append(value[14])
mantissa.append(value[15])
mantissa.append(value[16])
mantissa.append(value[17])
mantissa.append(value[18])
mantissa.append(value[19])
mantissa.append(value[20])
mantissa.append(value[21])
mantissa.append(value[22])
mantissa.append(value[23])
mantissa.append(value[24])
mantissa.append(value[25])
mantissa.append(value[26])
mantissa.append(value[27])
mantissa.append(value[28])
mantissa.append(value[29])
mantissa.append(value[30])
mantissa.append(value[31])
mantissa.append(value[32])
print (mantissa)


So rather than appending each index individually I was wondering if there is a way to add them to the list all in one line. I have tried the following
extend
methods:

exponent.extend(value[1, 2, 3, 4, 5, 6, 7, 8])
and without commas too

exponent.extend(value[1], value[2], value[3], value[4], value[5], value[6], value[7], value[8], )
which I then realised
extend
only take one argument.

exponent.extend(value[1-8])
which seem to of subtracted the one and eight.

I tried
exponent = {}
which is a set I believe? And then tried
exponent.update
followed by the multiple indexes with commas. Which then told me it only supports one argument.

Any other suggestions on how to add multiple indexes from
value
to a list?

Answer

Same code can be written using string slicing as:

value = input("Please enter 32 bit floating value to convert.")

exponent, mentissa= value[1:9], value[9:]
Comments