Python Forum
Convert python list to dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert python list to dictionary
#1
Sad 
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:


Output:
dict = {{'Dia': 'qua', 'Promo': 'João', 'Loja': 6697}, {'Dia': 'qui', 'Promo': 'João', 'Loja': 5599}, {'Dia': 'sex', 'Promo': 'João', 'Loja': 7437}}
Reply
#2
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
Reply
#3
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):
            print(f'\n{indent}{key}')
            level += 1
            display_dict(value, level)
        else:
            print(f'{indent}{key}: {value}')
        if level > 0:
            level -= 1


if __name__ == '__main__':
    display_dict(newdict)
output when run:
Output:
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
Reply
#4
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.
Reply
#5
Please how can I finish the topic as solved????
Reply
#6
just stop posting. All threads remain so that others may benefit from them in the future.
Reply
#7
# 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))}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert each element of a list to a string for processing tester_V 6 766 Jun-16-2021, 02:11 AM
Last Post: tester_V
  convert numbers into list lokesh 1 552 Jun-03-2021, 06:37 AM
Last Post: menator01
Question convert unlabeled list of tuples to json (string) masterAndreas 4 1,750 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Python dictionary with values as list to CSV Sritej26 4 754 Mar-27-2021, 05:53 PM
Last Post: Sritej26
Star Convert Bytearray into List using list() Shlok 2 629 Feb-18-2021, 10:44 AM
Last Post: deanhystad
  convert List with dictionaries to a single dictionary iamaghost 3 769 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  Creating a dictionary from a list Inkanus 5 1,120 Nov-06-2020, 06:11 PM
Last Post: DeaD_EyE
  convert list to five columns dataframe in sequence tonycat 2 847 Sep-29-2020, 06:47 AM
Last Post: tonycat
  Dictionary list coja56 7 1,000 Sep-20-2020, 03:53 PM
Last Post: deanhystad
  How to make a list of values from a dictionary list? faryad13 2 735 Sep-03-2020, 03:45 PM
Last Post: faryad13

Forum Jump:

User Panel Messages

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