-2

How do I join 2 lists with elements side by side? For example:

list1 = ["they" , "are" ,"really" , "angry"]  
list2 = ["they" , "are" ,"seriously" , "angry"] 

I want output as:

list3 = [("they","they"),("are","are"),("really","seriously"),("angry","angry")]

The above looks like a list tuples though, and if the above list were columns with each word in a row, how would I append list2 to list1?

3
  • 3
    Using zip. Commented Feb 13, 2014 at 11:53
  • 1
    this type of question has plenty in SO, please check. Commented Feb 13, 2014 at 11:59
  • 2
    Perhaps worth mentioning that if the lists are of uneven length, zip will discard the remaining elements of the longer list. You can use itertools.izip_longest if this is the case. Commented Feb 13, 2014 at 12:04

2 Answers 2

10

Use zip():

>>> list1 = ["they" , "are" ,"really" , "angry"]  
>>> list2 = ["they" , "are" ,"seriously" , "angry"] 
>>> list3 = zip(list1, list2)
>>> list3
[('they', 'they'), ('are', 'are'), ('really', 'seriously'), ('angry', 'angry')]
Sign up to request clarification or add additional context in comments.

1 Comment

what if i have 2 columns instead??
3

This is another solution,

>>> [ (val,list2[idx]) for idx, val in enumerate(list1)]
[('they', 'they'), ('are', 'are'), ('really', 'seriously'), ('angry', 'angry')]

By the way zip() is a good solution.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.