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
  Dictionary in a list bashage 2 495 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 597 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  How to add list to dictionary? Kull_Khan 3 951 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  convert string to float in list jacklee26 6 1,815 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  convert a list to links Pir8Radio 3 1,043 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  convert this List Comprehensions to loop jacklee26 8 1,418 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  check if element is in a list in a dictionary value ambrozote 4 1,879 May-11-2022, 06:05 PM
Last Post: deanhystad
  Convert list to interger Clives 5 1,542 May-09-2022, 12:53 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 1,900 Apr-28-2022, 06:59 AM
Last Post: buran

Forum Jump:

User Panel Messages

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