1

I want to use the join function in python (not any other function) to merge two lists into nested lists, assuming the lists are of equal length, for example:

list1 = [1, 2, 3]

list2 = ["a", "b", "c"]

I want it to produce a new list like this:

[[1,"a"], [2,"b"], [3,"c"]]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
user3208158
  • 19
  • 1
  • 7
  • 2
    What "join function" are you talking about? join is not a builtin function in python2. Do you mean some module's join function? Some object's join method? – Robᵩ Jan 17 '14 at 21:26
  • Haven't tried anything? – Bleeding Fingers Jan 17 '14 at 21:26
  • possible duplicate of [How to zip two lists of lists in Python?](http://stackoverflow.com/questions/7474717/how-to-zip-two-lists-of-lists-in-python) – Brad Jan 17 '14 at 21:27
  • 1
    Title says tuple and example expects list. How's that? – Bleeding Fingers Jan 17 '14 at 21:28
  • @Brad No, that problem is significantly different (more complicated). –  Jan 17 '14 at 21:28
  • possible duplicate of [python list of lists transpose without zip(\*m) thing](http://stackoverflow.com/questions/6473679/python-list-of-lists-transpose-without-zipm-thing) – inspectorG4dget Jan 17 '14 at 21:34
  • I didn't mean a tuple, and I want to do it using the join function in the string module – user3208158 Jan 17 '14 at 21:38
  • If you want to join a list of integers and a list of strings into a list of lists of integers and strings, `string.join` isn't going to help you much. Unsurprisingly, it's for joining strings together to make a new string. – DSM Jan 17 '14 at 21:49
  • So if the two lists were of strings, say each number was a string, how would you use string.join to interleave them like ["1","a"], ["2","b"] etc? – user3208158 Jan 17 '14 at 21:53
  • You still wouldn't. `string.join` accepts strings, and outputs a *string*. [You could use a string as an intermediate object, I guess, but it'd be pointless.] – DSM Jan 17 '14 at 22:06

3 Answers3

7

I don't think you understand what str.join is for.

str.join is a string method that takes an iterable (usually a list) of strings and returns a new string object that is a concatenation of those strings separated by the string that the method was invoked on.

Below is a demonstration:

>>> strs = ['a', 'b', 'c']
>>> ''.join(strs)
'abc'
>>> '--'.join(strs)
'a--b--c'
>>>

This means that you would not use str.join for what you are trying to do. Instead, you can use zip and a list comprehension:

>>> list1 = [1, 2, 3]
>>> list2 = ["a", "b", "c"]
>>> [list(x) for x in zip(list1, list2)]
[[1, 'a'], [2, 'b'], [3, 'c']]
>>>

Note however that, if you are on Python 2.x, you may want to use itertools.izip instead of zip:

>>> from itertools import izip
>>> list1 = [1, 2, 3]
>>> list2 = ["a", "b", "c"]
>>> [list(x) for x in izip(list1, list2)]
[[1, 'a'], [2, 'b'], [3, 'c']]
>>>

Like the Python 3.x zip, itertools.izip will return an iterator (instead of a list like the Python 2.x zip). This makes it more efficient, especially when dealing with larger lists.

  • @user3208158 - [`str.join`](http://docs.python.org/2.7/library/stdtypes.html#str.join) is a string method that returns a string, not a list. So no, you would not use it for this task. –  Jan 17 '14 at 22:15
1
list(map(list, zip(list1, list2)))
stewSquared
  • 827
  • 5
  • 24
0

Eureka! The join function you are looking for in this context, my friend, is the following:

def join(l1, l2):
    return [list(x) for x in zip(l1, l2)]

print join(list1, list2) 

:-)

Spade
  • 2,220
  • 1
  • 19
  • 29