Python Forum
Convert python list to dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert python list to dictionary
I need a help to convert the bellow list to a dictionary:

 list = [{'Dia': 'qua', 'Promo': 'João', 'Loja': 6697}, {'Dia': 'qui', 'Promo': 'João', 'Loja': 5599}, {'Dia': 'sex', 'Promo': 'João', 'Loja': 7437}]
The output must be:

dict = {{'Dia': 'qua', 'Promo': 'João', 'Loja': 6697}, {'Dia': 'qui', 'Promo': 'João', 'Loja': 5599}, {'Dia': 'sex', 'Promo': 'João', 'Loja': 7437}}
Right now you have a list that is made up of 3 dictionaries.

But the output that you are asking for is a "dictionary" made up of 3 dictionaries, but python will not allow that.

A dictionary is {"key": value}. So, the best you could do is to string your 3 dictionaries together.

Please correct me if I'm wrong. I often am :)
palladium likes this post
Nested dictionaries are allowed and useful
eachsub dictionary does require a key, so have made d1, d2 and d3
I added display dict so that you can display contents

newdict = {
    'd1': {'Dia': 'qua', 'Promo': 'João', 'Loja': 6697}, 
    'd2': {'Dia': 'qui', 'Promo': 'João', 'Loja': 5599}, 
    'd3': {'Dia': 'sex', 'Promo': 'João', 'Loja': 7437}

def display_dict(dictname, level=0):
    indent = " " * (4 * level)
    for key, value in dictname.items():
        if isinstance(value, dict):
            level += 1
            display_dict(value, level)
            print(f'{indent}{key}: {value}')
        if level > 0:
            level -= 1

if __name__ == '__main__':
output when run:
d1 Dia: qua Promo: João Loja: 6697 d2 Dia: qui Promo: João Loja: 5599 d3 Dia: sex Promo: João Loja: 7437
schascheck likes this post
Dictionaries need key/value pairs, so you can either make up a new key, or you can use one of the keys from the dictionaries. Larz60+ created a new key. Below the 'Dia' value is used as a key for each of the sub-dictionaries.
combined_dict = {'qua':{'Promo': 'João', 'Loja': 6697},
                 'qui':{'Promo': 'João', 'Loja': 5599},
                 'sex':{'Promo': 'João', 'Loja': 7437}}
You could also use the values for 'Loja' as the key for your sub-dictionaries. You cannot use 'Promo' values as a key for sub-dictionaries because the 'Promo' values are not unique.
Please how can I finish the topic as solved????
just stop posting. All threads remain so that others may benefit from them in the future.
# a list

mylist = [{'Dia': 'qua', 'Promo': 'João', 'Loja': 6697}, {'Dia': 'qui', 'Promo': 'João', 'Loja': 5599}, {'Dia': 'sex', 'Promo': 'João', 'Loja': 7437}]

# dict from list

mydict = {i:mylist[i] for i in range(len(mylist))}

Possibly Related Threads…
Thread Author Replies Views Last Post
  check if element is in a list in a dictionary value ambrozote 4 740 May-11-2022, 06:05 PM
Last Post: deanhystad
  Convert list to interger Clives 5 631 May-09-2022, 12:53 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 760 Apr-28-2022, 06:59 AM
Last Post: buran
  convert a named tuple to a dictionary Skaperen 13 1,214 Mar-31-2022, 07:13 PM
Last Post: Skaperen
  how to assign items from a list to a dictionary CompleteNewb 3 712 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 1,163 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,014 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Convert each element of a list to a string for processing tester_V 6 2,806 Jun-16-2021, 02:11 AM
Last Post: tester_V
  convert numbers into list lokesh 1 1,587 Jun-03-2021, 06:37 AM
Last Post: menator01
Question convert unlabeled list of tuples to json (string) masterAndreas 4 4,787 Apr-27-2021, 10:35 AM
Last Post: masterAndreas

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020