CosmoSurreal
CosmoSurreal

Reputation: 259

Python: Merging dictionary lists

I have lists inside a dictionary:

Number_of_lists=3           #user sets this value, can be any positive integer
My_list={}
for j in range(1,Number_of_lists+1):
  My_list[j]=[(x,y,z)]  

The Number_of_lists is a variable set by the user. Without knowing beforehand the value set by the user, i would like to finally have a merged list of all dictionary lists. For example if Number_of_lists=3 and the corresponding lists are My_list[1]=[(1,2,3)] , My_list[2]=[(4,5,6)] , My_list[3]=[(7,8,9)] the result would be:

All_my_lists=My_list[1]+My_list[2]+My_list[3]

where: All_my_lists=[(1,2,3),(4,5,6),(7,8,9)].

So what i'm trying to do is automate the above procedure for all possible:

Number_of_lists=n #where n can be any positive integer

I'm a bit lost up to now trying to use an iterator to add the lists up and always fail. I'm a python beginner and this is a hobby of mine, so if you answer please explain everything in your answer i'm doing this to learn, i'm not asking from you to do my homework :)

EDIT

@codebox (look at the comments below) correctly pointed out that My_List as displayed in my code is in fact a dictionary and not a list. Be careful if you use any of the code.

Upvotes: 5

Views: 1047

Answers (4)

Shawn Chin
Shawn Chin

Reputation: 86924

It might be easier to generate All_my_lists first followed by My_list.

Building All_my_lists

Using list comprehension and range() to generate All_my_lists:

>>> num = 3  # for brevity, I changed Number_of_lists to num
>>> All_my_lists = [tuple(range(num*i + 1, num*(i+1) + 1)) for i in range(0, num)]
>>> All_my_lists
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

Alternatively, we can use the grouper() function from list of itertools recipe which will result in a much cleaner code:

>>> All_my_lists = list(grouper(num, range(1, num*3+1)))
>>> All_my_lists
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

Building My_lists

We can then use dict constructor along with list comprehension and enumerate() to build derive My_list from All_my_list:

>>> My_lists = dict((i+1, [v]) for i,v in enumerate(All_my_lists))
>>> My_lists
{1: [(1, 2, 3)], 2: [(4, 5, 6)], 3: [(7, 8, 9)]}
>>> My_lists[1]
[(1, 2, 3)]
>>> My_lists[2]
[(4, 5, 6)]
>>> My_lists[3]
[(7, 8, 9)]

Upvotes: 1

Benedict
Benedict

Reputation: 2821

You could try a more functional approach by turning Number_of_lists into a sequence of keys using range and select out of the dictionary with map:

My_list={1:[1,2,3], 2:[4,5,6], 3:[7,8,9], 4:[10,11,12]}
Number_of_lists=3
All_my_lists=map(lambda x: tuple(My_list[x]), range(1, Number_of_lists+1))

Example output:

>>> All_my_lists
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

Upvotes: 1

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251096

use a list comprehension:

>>> Number_of_lists=3 
>>> My_list={}
>>> for j in range(1,Number_of_lists+1):
      My_list[j]=(j,j,j)
>>> All_my_lists=[My_list[x] for x in My_list]

>>> print(All_my_lists)
[(1, 1, 1), (2, 2, 2), (3, 3, 3)]

All_my_lists=[My_list[x] for x in My_list] is equivalent to:

All_my_lists=[]
for key in My_list:
   All_my_lists.append(My_list[key])

Upvotes: 1

codebox
codebox

Reputation: 20254

If you are only concerned with the final list, and don't actually need My_list (which you should rename, because its a dictionary!) then you could just do:

Number_of_lists=3
result = []
for j in range(1,Number_of_lists+1):
    result += (x,y,z)

Upvotes: 1

Related Questions