+
implies concatenation of objects, so intuitively:
[5, 6, 7] + [8, 9]
= [5, 6, 7, 8, 9]
As mentioned by Darkchili Slayer, you embed lists in another list by appending. In fact, a rather simple solution is to just do:
Python 3.4.2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [5, 6, 7]
>>> b = [8, 9]
>>> c = []
>>> c.append(a)
>>> c.append(b)
>>> c
[[5, 6, 7], [8, 9]]
If you want to get fancy, you can do something like this using the special variable argument operator, *:
>>> def join_l(*lists):
... temp = []
... for l in lists:
... temp.append(l)
... return temp
...
>>> join_l([5, 6, 7], [8, 9])
[[5, 6, 7], [8, 9]]
You can even do it this way too, and make it a bit easier to read:
def join_l(*lists):
... return list(lists)
...
>>> join_l([5, 6, 7], [8, 9])
[[5, 6, 7], [8, 9]]
Finally, it's worth noting that there is an extend
function for lists, which appends each item in another list. You can use this to simplify the first example:
>>> a = [5, 6, 7]
>>> b = [8, 9]
>>> c = []
>>> c.extend([a, b])
>>> c
[[5, 6, 7], [8, 9]]
In this case, the extend
function isn't very useful because the input is exactly the same as its output.