1

I performing concatenation of multiple variables to a string and trying to store it in a list. I want the list to have the string but when i append the list in a loop, the value on the list is shown as tuple. Please help, New to python :)

When i Print :

print ( key +' ' +' =', amount1+' ' +' /',amount2+' ' +' /',pincrease)

Output is in str :

app1  = 53.58  / 54.81  / 2.24% lower.

When i append my list :

message = ( key +' ' +' =', amount1+' ' +' /',amount2+' ' +' /',pincrease)
message.append(mylist)
print(mylist)

output :

[('app1  =', '53.58  /', '54.81  /', '2.24% lower.')]

I would like to get the value as string in the list.. as

["app1  = 53.58  / 54.81  / 2.24% lower."]
Lucas Hendren
  • 2,786
  • 2
  • 18
  • 33
Strike
  • 95
  • 1
  • 1
  • 8

2 Answers2

2

You code is actually working fine, but there is a some small mistake when you append your message into the list.

mylist.append(''.join(message))  # this step will remove your tuple
print(mylist)

# ["app1  = 53.58  / 54.81  / 2.24% lower."]
Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
1

First, this code:

( key +' ' +' =', amount1+' ' +' /',amount2+' ' +' /',pincrease)

is a tuple. You might consider using format strings instead of + for this kind of thing. Which would make it look more like this:

message = f'{key} = {amount1} / {amount2} / {pincrease}'
mylist.append(message)
Joshua Smith
  • 6,561
  • 1
  • 30
  • 28