0

I have used regular expressions to parse a file and locate the name and price of the following currencies. Note that both of these items (x=name/y=price) inserted into lists. I have used a join statement on top of this:

print("\n".join(x + (y)))
"name":"Bitcoin"

"name":"Ethereum"

"name":"Tether"

"price":21335.75253

"price":1592.06118

"price":1.00015`

From here, I am trying to get the following display:

"name":"Bitcoin" - "price":21335.75253

"name":"Ethereum" - "price":1592.06118

"name":"Tether" - "price":1.00015`

The only way I can get the intended display, is by using the following:

print(x[0],y[0])

"name":"Bitcoin" "price":21335.75253

Ideally I can use a for loop inside both slices, but I'm not sure thats possible.

DYZ
  • 55,249
  • 10
  • 64
  • 93
John C
  • 45
  • 4

4 Answers4

1

"zip" should do the trick.

for name, price in zip(x, y):
    print('"name":"{}" - "price":{}'.format(name, price))
iohans
  • 838
  • 1
  • 7
  • 15
0

You can use the zip function that combines any number of lists together. It's important that the longest list is the first argument in the function call so you can combine it like print("\n".join(list(zip(x, y))))

0

Try

x = ['"name":"Bitcoin"', '"name":"Ethereum"', '"name":"Tether"']
y = ['"price":21335.75253', '"price":1592.06118', '"price":1.00015']
print('\n'.join([f'{name} - {price}' for name, price in zip(x,y)]))
Zach Flanders
  • 1,224
  • 1
  • 7
  • 10
0

try

print('\n'.join(f'{i[0]} - {i[1]}' for i in zip(x, y)))
zeewin
  • 1
  • 1