0

I have a list like this :

{'serial1': 'BPCE-RNHC-25G8', 'subject1': 'EMP004', 'serial2': '7QF2-6HI9-XKZZ', 'subject2': 'EMP005', 'serial3': '8FIW-9JRB-NY4J', 'subject3': 'EMP003', 'serial4': 'SG8P-YQKG-ZV3C', 'subject4': 'EMP002', 'serial5': 'PBF7-WZ
HT-WPZR', 'subject5': 'EMP001'}

When I send this list over my html template using for loop , the serial and subject value is separated and divide into 2 column which represent: enter image description here

My desire output is the serial and subject appear on the same column for the same record. For example: enter image description here

As you can see my dict has serial1,2,3.. and subject 1,2,3... How can I combine the serial(n) with subject(n) as one item in the json list? So that i could display it on my HTML as 1 item?

1
  • 2
    First thing is, what you are showing is not a list, its Python dictionary Commented Oct 26, 2021 at 8:42

4 Answers 4

1

If the keys are only subjectN and serialN, and N <= len(my_dict)/2 (ie. there are no other keys, you don't skip any numbers), we can just merge them nicely.

my_dict = {'serial1': 'BPCE-RNHC-25G8', 'subject1': 'EMP004', 'serial2': '7QF2-6HI9-XKZZ', 'subject2': 'EMP005', 'serial3': '8FIW-9JRB-NY4J', 'subject3': 'EMP003', 'serial4': 'SG8P-YQKG-ZV3C', 'subject4': 'EMP002', 'serial5': 'PBF7-WZHT-WPZR', 'subject5': 'EMP001'}

merged = {f'merged{i}': my_dict[f'subject{i}']+': '+my_dict[f'serial{i}'] for i in range(1, len(my_dict)//2+1)}

You can rename your new key. If you want it as a list:

merged = [my_dict[f'subject{i}']+': '+my_dict[f'serial{i}'] for i in range(1, len(my_dict)//2+1)]

What this does:

i goes from 1 to len(my_dict)//2 (inclusive; range end is exclusive)

resulting list/dict is just a string concatenation of subject with given number, ': ', and serial with given number.

Result on the given example

>>> merged = {f'merged{i}': my_dict[f'subject{i}']+': '+my_dict[f'serial{i}'] for i in range(1, len(my_dict)//2+1)}
>>> merged
{'merged1': 'EMP004: BPCE-RNHC-25G8', 'merged2': 'EMP005: 7QF2-6HI9-XKZZ', 'merged3': 'EMP003: 8FIW-9JRB-NY4J', 'merged4': 'EMP002: SG8P-YQKG-ZV3C', 'merged5': 'EMP001: PBF7-WZHT-WPZR'}
>>>
>>> merged = [my_dict[f'subject{i}']+': '+my_dict[f'serial{i}'] for i in range(1, len(my_dict)//2+1)] 
>>> merged
['EMP004: BPCE-RNHC-25G8', 'EMP005: 7QF2-6HI9-XKZZ', 'EMP003: 8FIW-9JRB-NY4J', 'EMP002: SG8P-YQKG-ZV3C', 'EMP001: PBF7-WZHT-WPZR']
Sign up to request clarification or add additional context in comments.

Comments

1

If you just want to join the key/value pairs by : , you can use join for each key/value pair in the iteration using dict.items(), you can do it in a List-Comprehension to do it for the entire dictionary.

>>> [': '.join(t) for t in d.items()]

['serial1: BPCE-RNHC-25G8', 'subject1: EMP004', 'serial2: 7QF2-6HI9-XKZZ', 'subject2: EMP005', 'serial3: 8FIW-9JRB-NY4J', 'subject3: EMP003', 'serial4: SG8P-YQKG-ZV3C', 'subject4: EMP002', 'serial5: PBF7-WZHT-WPZR', 'subject5: EMP001']

Comments

1

I hope this code helps you

a={'serial1': 'BPCE-RNHC-25G8', 'subject1': 'EMP004', 'serial2': '7QF2-6HI9-XKZZ', 'subject2': 'EMP005', 'serial3': '8FIW-9JRB-NY4J', 'subject3': 'EMP003', 'serial4': 'SG8P-YQKG-ZV3C', 'subject4': 'EMP002', 'serial5': 'PBF7-WZHT-WPZR', 'subject5': 'EMP001'}

a=list(a.items())

for i in range(0,len(a),2):
    print(a[i+1][1]+' : '+a[i][1])

The output is

EMP004 : BPCE-RNHC-25G8
EMP005 : 7QF2-6HI9-XKZZ
EMP003 : 8FIW-9JRB-NY4J
EMP002 : SG8P-YQKG-ZV3C
EMP001 : PBF7-WZHT-WPZR

Comments

1

You can loop through the dictionary and print values as given below:

dictValue = {'serial1': 'BPCE-RNHC-25G8', 'subject1': 'EMP004', 'serial2': '7QF2-6HI9-XKZZ',  'subject2': 'EMP005', 'serial3': '8FIW-9JRB-NY4J', 'subject3': 'EMP003',  'serial4': 'SG8P-YQKG-ZV3C',  'subject4': 'EMP002',  'serial5': 'PBF7-WZHT-WPZR', 'subject5': 'EMP001' }
n = int(len(dictValue)/2)
for i in range(1,n+1):
    print(dictValue.get('subject'+str(i)),':', dictValue.get('serial'+str(i)))
EMP004 : BPCE-RNHC-25G8
EMP005 : 7QF2-6HI9-XKZZ
EMP003 : 8FIW-9JRB-NY4J
EMP002 : SG8P-YQKG-ZV3C
EMP001 : PBF7-WZHT-WPZR

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.