Skip to content

Instantly share code, notes, and snippets.

@chooper
Created June 29, 2011 20:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chooper/1054819 to your computer and use it in GitHub Desktop.
Save chooper/1054819 to your computer and use it in GitHub Desktop.
Merging list of lists in Python using reduce()
#!/usr/bin/env python
"""Merging list of lists in Python using reduce()"""
def merge_lists(list_of_lists):
return reduce(lambda x,y: x+y, list_of_lists)
if __name__ == '__main__':
my_big_list = [ [1,2,3], [3,4,5], [6,7,8], ]
print merge_lists(my_big_list)
# output: [1, 2, 3, 3, 4, 5, 6, 7, 8]
@shuishu
Copy link

shuishu commented Feb 17, 2024

I tested this code in a large list data , waited a long time, but no result were returned. i used following funtion, and i got the result very quickly. i don't know why.

new_list=[]
for i in my_big_list :
new_list.extend(i)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment