-4

How do I add two lists, such that the resulting list keeps the other lists intact:

['5','6','7'] + ['1'] + ['9','7'] = [['5','6','7'], ['1'], ['9','7']]

Is it possible to do this in python?

Current code:

def appendy(list_o_list): 
    temp_l = [] 
    for l in list_o_list: 
        temp_l.append(list(l)) 
        new_list=[] 
        new_list = [a + b for a, b in itertools.combinations(temp_l, 2)]                    
        print("app",new_list) 
return (new_list) 

appendy([('g3', 'g1'), ('g3', 'g2')])
Wyetro
  • 8,439
  • 9
  • 46
  • 64
Satyam Raha
  • 123
  • 1
  • 8

2 Answers2

1

Its not adding lists, it is appending lists. Its quite easy to just do with .append()

Just do:

resulting_list = []
resulting_list.append(lista)
resulting_list.append(listb)
resulting_list.append(listc)

All the original lists will remain the same and resulting_list will contain the joined lists. What you are trying to do is not completely clear.

Octo
  • 695
  • 6
  • 20
  • def appendy(list_o_list): temp_l=[] for l in list_o_list: temp_l.append(list(l)) new_list=[] new_list = [a + b for a, b in itertools.combinations(temp_l, 2)] print("app",new_list) return (new_list) appendy([('g3', 'g1'), ('g3', 'g2')]) – Satyam Raha Dec 25 '16 at 21:10
  • 3
    @SatyamRaha why would you think posting your code in a comment is any-way reasonable? – juanpa.arrivillaga Dec 25 '16 at 21:11
  • its kinda messy...may be u can get some idea about what i am trying to do. appendy finds all possible permutations from a list "list_o_list" – Satyam Raha Dec 25 '16 at 21:12
  • i did post my code to just let you know that your solution doesn't really solve my problem – Satyam Raha Dec 25 '16 at 21:13
  • 1
    @SatyamRaha, post your code as an edit to your question. The comments is not the right place for that. I added it this time. – Wyetro Dec 25 '16 at 21:21
0

+ 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.

Community
  • 1
  • 1
Aaron3468
  • 1,734
  • 16
  • 29