-1

I want to merge mutable lists into one. I am using speech recognition, so the lists of words that I am getting are changing all the time. Some people said to add lists using the + operator, but every time the speaker speaks, various numbers of lists are generated. Think as if the program doesn't already know how many lists there will be every time. I have lists such as ['hi'] and ['hello'] and the next time I have lists such as ['one'], ['two'] and ['three']. I want to write a code or a function that allows to add all numbers of lists into one. Thus, I want to implement the same code to all numbers of lists. In the end I want to have ['hi', 'hello'] or ['one', 'two', 'three']. I am very new to Python. Thank you in advance!

  • 2
    "Some people" were right, use +. Or explain clearly why + doesn't work for you. – Julien Aug 01 '16 at 19:25
  • 2
    It's very unclear what you're asking for, and why concatenating lists using `+` isn't exactly what you want. If you get different lists at different times, presumably you just want to concatenate each of those times. Have you written any code at all that you can show, to anchor your question in something concrete? – Blckknght Aug 01 '16 at 19:26
  • Possible duplicate of [How to append list to second list (concatenate lists)](http://stackoverflow.com/questions/1720421/how-to-append-list-to-second-list-concatenate-lists) – be_good_do_good Aug 01 '16 at 19:51
  • @JulienBernu I apologize for making it unclear as I am new to coding. The reason why it might not work right now is because I want to write a function that can merge lists for as many lists as the user gives in speech recognition. What I mean by that is, I want to have a pre-existing code that when a user records and then runs the code (my code currently separates words into lists from a for loop due to something specific I want to do later), python returns a final list. So one time I can have 5 lists, and the next time I can have 32. My main goal is to concatenate them into one. – mathimatika Aug 01 '16 at 21:31
  • @Blckknght so far my code is list=array.split(",") #that creates separate lists. – mathimatika Aug 01 '16 at 21:56
  • Possible duplicate of [Making a flat list out of list of lists in Python](http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python) – Julien Aug 02 '16 at 04:50
  • have you tried loops? – noɥʇʎԀʎzɐɹƆ Aug 02 '16 at 14:49
  • @vick please format your code using `code blocks` using backticks (\`). And why go for speech recognition as a beginner? – noɥʇʎԀʎzɐɹƆ Aug 02 '16 at 18:08

2 Answers2

0

You have three approaches to merge lists:

# using +
a = ['A']
b = [1, 2]  

a + b # ['A', 1, 2]

# using append
a.append(b) # ['A', [1, 2]]

# using extend
a.extend(b) # ['A', 1, 2]

What's the difference between them? + operator creates new list, whereas methods mutate the one on which you call them.

PatNowak
  • 5,721
  • 1
  • 25
  • 31
  • I apologize for not explaining what I mean 'mutating'. What I mean by that is that I want to create a code that can take a various number of lists and merge them into one. In your code, you already know that there are only two lists, but what if you didn't know the total number of lists like in speech recognition? Thank you for your help! – mathimatika Aug 01 '16 at 21:34
0

I believe the + operator is exactly what you want. For example:

a = ["foo", "bar"] 
b = ["bar", "got"]
c = a + b

prints ['foo', 'bar', 'bar', 'got']


If you want to add b to a use .extend(). You could also use += but it's arguably less pythonic.

a = ["foo", "bar"]
b = ["bar", "got"]
a.extend(b)  # (or a += b)
print(a)

prints ["foo", "bar", "bar", "got"]

UPDATED: With new information from the author, here's my four-line solution:

def merge(lists):
    result = []
    for list in lists:
        result.extend(list)
    return result

or, alternatively using functools.reduce() and the operator module:

import functools, operator
def merge(lists):
    return functools.reduce(operator.add, lists)

functools.reduce() simply does:

Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence.

and operator.add() is a shortcut for lambda x, y: x + y.

noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67
  • You see I want to create a code or function can take a various number of lists and merge them into one. In your code, you already know that there are only two lists, but what if you didn't know the total number of lists like in speech recognition? Thanks! – mathimatika Aug 01 '16 at 21:36
  • @vick I did just that for you in my updated answer! – noɥʇʎԀʎzɐɹƆ Aug 02 '16 at 03:02
  • Thank you! I will try to use them and see whether they work! Thank you for your help and I apologize for the late reply! – mathimatika Aug 02 '16 at 17:13
  • @vick don't forget to accept my answer by clicking the check mark below the voting thing if it was helpful. You should be able to test this quickly... – noɥʇʎԀʎzɐɹƆ Aug 02 '16 at 18:04
  • @vick my code had a small bug - I forgot to return the result! please try my solution again, I fixed it – noɥʇʎԀʎzɐɹƆ Aug 02 '16 at 18:05
  • @ uoɥʇʎPʎzɐɹC I tried them separately but they didn't work, I also tried your changed version, I will see whether there is an issue with my code. Thanks again! – mathimatika Aug 02 '16 at 18:28
  • @vick does it work now? – noɥʇʎԀʎzɐɹƆ Aug 02 '16 at 18:34
  • @ uoɥʇʎPʎzɐɹC I decided to go ahead and instead of using lists to use dictionaries. I will see what this can do, I think that my initial methodology and approach was wrong. – mathimatika Aug 02 '16 at 20:48