2

Hi I am trying compile a bunch of arrays that I have in a dictionary using a for loop and I cant seem to find proper solution for this.

Essentially what I have in a simplified form:

dict['w1']=[1,2,3]
dict['w2']=[4,5,6]
dict['w3']=[7,8]

x = []
for i in range(3):
    x = np.concatenate([x],[dict['w'+str(i+1)].values],axis=0)

what it gives:

x = [1,2,3,4,5,6,7,8]

what I want:

x = [[1,2,3],[4,5,6],[6,7]]

I want to use the for loop because I have too many arrays to 'compile' and cant key in them one by one would be very inefficient. This way I can use the created array to plot a boxplot directly.

A simiiliar question exists with no loop requirement but still does not have proper solution. Link

4
  • You won't be able to achieve that solution with a numpy array, all column and rows have to be the same size in a np.array. That being said you seem to be confusing numpy arrays and python lists they are not the same, so don't use numpy functions (wich work on arrays) if you only use lists. Commented Oct 8, 2016 at 12:41
  • 1
    Last element of expected o/p has to be [7,8] instead, right? Commented Oct 8, 2016 at 12:58
  • Also, I sensed that you are looking to avoid loop from your question's other parts, except at this line : "I want to use the for loop ...". Typo there? Commented Oct 8, 2016 at 13:07
  • 1
    I'm removing the duplicate; the answers in stackoverflow.com/questions/9285414/appending-to-a-nested-list are inferior to the answers already given (and accepted) here. Commented Oct 8, 2016 at 18:22

4 Answers 4

2

Comprehension:

# using name as "dict" is not proper use "_dict" otherwise.
dict['w1']=[1,2,3]
dict['w2']=[4,5,6]
dict['w3']=[7,8]

x = [dict["w"+str(i)] for i in range(1, 4)]

gives output:

[[1, 2, 3], [4, 5, 6], [7, 8]]
Sign up to request clarification or add additional context in comments.

2 Comments

This works like a charm! I have a question though. I have the keys in the dict as 'w01', 'w02'.......'w10'. So there is slight issue when its hits the value '10'. Is it possible to include a 'if' into the code u have written above?
You can use condition in Comprehensions, it'll go something like [x1 for x1 in something if condition == True else x2].
1

One approach would be to get the argsort indices based on the keys and sort the elements/values out of the dictionary using those indices, like so -

np.take(dict.values(),np.argsort(dict.keys()))

If you need a list as the output, add .tolist().

Sample run -

In [84]: dict
Out[84]: {'w1': [4, 8, 2, 1], 'w2': [1, 3, 8], 'w3': [3, 2]}

In [85]: np.take(dict.values(),np.argsort(dict.keys()))
Out[85]: array([[4, 8, 2, 1], [1, 3, 8], [3, 2]], dtype=object)

In [86]: np.take(dict.values(),np.argsort(dict.keys())).tolist()
Out[86]: [[4, 8, 2, 1], [1, 3, 8], [3, 2]]

Comments

0

Just append the item to the list. Note, BTW, that range starts with 0 if you don't specify the start value and treats the end value as an exclusive border:

x = []
for i in range(1, 4):
    x.append(dict['w' + str(i)])

Comments

0

If you mean lists and not dictionarys, then you're looking for the zip command.

Zip (w1,w2,w3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.