0

I have a list:

Marks =[
    [12345,75,'English'],
    [23452,83,'Physics'],
    [23560,81,'Statistics'],
    [23415,61,'Computer'],
    [23459,90,'Physics'],
    [12345,75,'Computer'],
    [23452,100,'Statistics']
]

and I want to make a dictionary where the class name is the key and the two numbers are the values. I can make the initial dictionary like this:

for i in list1:
    dict1[i[2]]=i[0],i[1]

But when I print the dictionary, only one of each of the pairs of numbers associated is printed. I'm looking for a result like:

{ 
  'English' : [[12345,75]],
  'Physics' : [[23452,83], [23459,90]],
  'Statistics' : [[23560,81], [23452,100]],
  'Computer' : [[23415,61], [12345,75]]
}

Is there any way I can extend the values associated with a key?

I tried:

if i[2] in dict1:
    dict1.extend
4
  • 1
    print( {i[-1]: i[:2] for i in Marks} ) ? Commented Apr 9, 2019 at 12:10
  • The issue that I'm having is that there would be two sets of tuples for the same course. It should be 'Statistics: [23560, 81], [23452, 100]. And I can add the first set of numbers, but not hte second Commented Apr 9, 2019 at 12:17
  • @user10613120 It's a good idea to include an example of the output you're looking for. I've edited in an example. Commented Apr 9, 2019 at 12:22
  • {'Computer': [12345, 75], [23145, 65] , 'English': [12345, 75], 'Physics': [23459, 90], [12345,67], 'Statistics': [23452, 100], [98765,30]} Commented Apr 9, 2019 at 12:25

3 Answers 3

2

Using collections.defaultdict

Ex:

from collections import defaultdict

Marks = [[12345,75,'English'], [23452,83,'Physics'], [23560,81,'Statistics'], [23415,61,'Computer'], [23459,90,'Physics'], [12345,75,'Computer'], [23452,100,'Statistics']]

result = defaultdict(list)
for i in Marks:
    result[i[-1]].append(i[:2])
print(result)

Output:

defaultdict(<type 'list'>, {'Statistics': [[23560, 81], [23452, 100]], 'Physics': [[23452, 83], [23459, 90]], 'Computer': [[23415, 61], [12345, 75]], 'English': [[12345, 75]]})
Sign up to request clarification or add additional context in comments.

Comments

1

You can only have a single value for a key in your dictionary. But you could save the two values as either a list or a tuple like so:

for i in list1:
    dict1[i[2]]=i[0:1]

or

for i in list1:
    dict1[i[2]]=(i[0], i[1])

1 Comment

but there are two sets of two numbers associated with each course. My poor explanation, but that's the issue that I'm having. It should be 'Statistics: [23560, 81], [23452, 100]
1

You can use comprehensions to achieve that easily:

>>> Marks = [[12345,75,'English'], [23452,83,'Physics'], [23560,81,'Statistics'], [23415,61,'Computer'], [23459,90,'Physics'], [12345,75,'Computer'], [23452,100,'Statistics']]
>>> d = { m[2]: [m[0], m[1]] for m in Marks }
>>> d
{'English': [12345, 75], 'Physics': [23459, 90], 'Statistics': [23452, 100], 'Computer': [12345, 75]}
>>> 

EDIT: This will create the dictionary taking into account those repeated keys.

d = {}
for mark in Marks:
     if not mark[2] in d:
             d[mark[2]] = [[mark[0], mark[1]]]
     else:
             d[mark[2]] += [[mark[0], mark[1]]]

The dictionary will end up with this shape:

>>> d
{'English': [[12345, 75]], 'Physics': [[23452, 83], [23459, 90]], 'Statistics': [[23560, 81], [23452, 100]], 'Computer': [[23415, 61], [12345, 75]]}
>>> 

4 Comments

but there are two sets of two numbers associated with each course. My poor explanation, but that's the issue that I'm having. It should be 'Statistics: [23560, 81], [23452, 100]
Sorry. I completely missed that detail. I will try to update my example.
Perfect! This is what I was trying to do with either extending or adding to the value. Thanks!
Take a look on Rakesh's answer. It is a better and more Pythonic way of achieving the same result.

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.