0

What I have is

brand = ['Apple','Oppo','Huawei']
model = ['IPhone SE', 'K10 5G', 'Honor X7']
price = ['400','380','160']

and I was looking for the output to be

brand : Apple
model : Iphone SE
price : '400'

brand : Oppo
model : K10 5G
price : '380'

brand : Huawei
model : Honor X7
price : '160'

I tried

brand = ['Apple','Oppo','Huawei']
model = ['IPhone SE', 'K10 5G', 'Honor X7']
price = ['400','380','160']

    for i in brand:
      for j in model:
        for k in price:
          print('brand : ' + i + '\nmodel : ' + i + '\nprice : ' + k) 

error says that it can only concatenate from str not list.

would appreciate any help, I am not good in dealing with string :/

LuckyCoin
  • 19
  • 4

2 Answers2

1

The zip() builtin function was designed for exactly this task:

brands = ['Apple','Oppo','Huawei']
models = ['IPhone SE', 'K10 5G', 'Honor X7']
prices = ['400','380','160']

for brand, model, price in zip(brands, models, prices):
    print('brand :', brand)
    print('model :', model)
    print('price :', price)
    print()
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
0

To iterate through corresponding pairs/triples/etc. of items in different lists, you can use zip().

Additionally, using an f-string is easier than using concatenation here.

for (i, j, k) in zip(brand, model, price):
    print(f'brand : {i}\nmodel : {j}\nprice : {k}')
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53