1

If I have 2 lists:
x = [1, 2, 3, 4]
y = [a, b, c, d]
How can I display something like:
1 - a, 2 - b, 3 - c, 4 - d?

dwnppoalt
  • 11
  • 1
  • Check out [`zip`](https://docs.python.org/3/library/functions.html#zip) to iterate over both lists together. You also have the option to iterate over the range of indices – shriakhilc Jan 04 '22 at 11:37
  • Does this answer your question? [How to iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel) – Syed Shahzer Jan 04 '22 at 11:37
  • Syed Shahzer This is what I was looking for. Thanks! – dwnppoalt Jan 04 '22 at 11:38

2 Answers2

1
print([str(x[i]) + "-" + str(y[i]) for i in range(len(x))])
sm3sher
  • 2,764
  • 1
  • 11
  • 23
0

Use this code if your two list's lengths are same.

a = [1,2,3,4]
b = ['a', 'b', 'c', 'd']
st = ""
for i, j in zip(a,b):
    st += f", {i} - {j} "
new_st = st.lstrip(",")
print(new_st)
Sunjid
  • 72
  • 3