0

I have two list of list data and I am trying to concatenate the two list of data in python. I used regular expression in extracting data from my desired file and collected the required fields by creating a list. For example if the list 1 and list 2 has data:

lst1 = [['60', '27702', '1938470', '13935', '18513', '8'], ['60', '32424', '1933740', '16103', '15082', '11'], ['60', '20080', '1946092', '9335', '14970', '2']]

lst2 = [['2', '1'], ['11', '1'], ['12', '1']]

I will like to see the data like below:

lst3 = [[60, 27702, 1938470, 13935, 18513, 8, 2, 1],

[60, 32424, 1933740, 16103, 15082, 11, 11, 1],

[60, 20080, 1946092, 9335, 14970, 2, 12, 1]]

How do I receive lst3 ??

3

1 Answer 1

3

Just interleave both sublists using zip, and convert the joined list members to integers in a list comprehension:

lst3 = [[int(x) for x in l1+l2] for l1,l2 in zip(lst1,lst2)]

result

[[60, 27702, 1938470, 13935, 18513, 8, 2, 1],
[60, 32424, 1933740, 16103, 15082, 11, 11, 1],
[60, 20080, 1946092, 9335, 14970, 2, 12, 1]]
Sign up to request clarification or add additional context in comments.

4 Comments

hi, can I see the data row wise ? like each of the sub list in rows.
you mean like [(60, 60, 60), (27702, 32424, 20080), (1938470, 1933740, 1946092), (13935, 16103, 9335), (18513, 15082, 14970), (8, 11, 2), (2, 11, 12), (1, 1, 1)] ?
not actually. Like : 60, 27702, 1938470, 13935, 18513, 8, 2, 1 in the first row then 60, 32424, 1933740, 16103, 15082, 11, 11, 1 in the next row...
that's already the case. The result is a list of rows. Put that in a csv using writerows you get your rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.