-1

I'm quite new to python... so if

string_1 = ["a", "b", "c", "d"]
string_2 = [1,2,3,4]

how can I make it so it would print out:

a equals 1
b equals 2
c equals 3
d equals 4

I've tried:

for i in string_1:
    for j in string_2:
    print(i, "equals", j)
k wn
  • 3
  • 2
  • 1
    Will the length of both the string stay same all the time ? – zenwraight Jan 23 '18 at 23:31
  • 2
    Please check the indentation of code. Also why the name `string_n`? Those are lists and besides stating types in names is not helpful as it may seem at first. – progmatico Jan 23 '18 at 23:36

3 Answers3

3

It would be better to use a dictionary for something like this.

E.g.

things = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

for k, v in things.items():
  print(k, 'equals', v)
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
2

This is a job for zip

items_1 = ["a", "b", "c", "d"]
items_2 = [1,2,3,4]

for a, b in zip(items_1, items_2):
    print ("{0} equals {1}".format(a, b)
clement
  • 21
  • 1
  • 5
0

As others have already said, python has zip for just this purpose. However, if you insist on doing this without zip:

for i in range(len(string_1)):
    print(string_1[i] + ' equals ' + string_2[i])

will accomplish this.

Alan Hoover
  • 1,430
  • 2
  • 9
  • 13