-1

I am trying to combine two list based on user input using the zip() function. But I want the result to not have the extra brackets.

lst1 = []

# For list of strings/chars
lst2 = []

lst1 = [int(item) for item in input("Enter the list items : ").split()]

lst2 = [int(item) for item in input("Enter the list items : ").split()]

print(lst1)
print(lst2)

print([list(a) for a in zip(lst1,lst2)])
Output = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]
Desired output = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
Алексей Р
  • 7,507
  • 2
  • 7
  • 18
Usman
  • 1

2 Answers2

1

See this answer.

print([e for t in zip(lst1, lst2) for e in t])
StSav012
  • 776
  • 5
  • 15
1
from itertools import chain

lst1 = []

# For list of strings/chars
lst2 = []

lst1 = [int(item) for item in input("Enter the list items : ").split()]

lst2 = [int(item) for item in input("Enter the list items : ").split()]

# fist method
print(list(chain.from_iterable([a for a in zip(lst1,lst2)])))

# second method
total_list = []
for a, b in zip(lst1,lst2):
    total_list.append(a)
    total_list.append(b)
std124_lf
  • 134
  • 2
  • 9