Create keys for dictionary based on length of list of lists

python dictionary with list as value
python create dictionary from list
python convert list to dictionary keys
convert list of dictionaries to dictionary python
create dictionary python
python create dictionary from list of values
convert list of key-value pairs to dictionary python
python create dictionary from two lists

I have the following list:

my_list = [[['pd', 1],
           ['pd_de', None],
           ['pd_amnt', '$10.00']],
           [['pd', 1],
           ['pd_de', '5/1/19 '],
           ['pd_amnt', '$100.00 '],
           ['pd', 2],
           ['pd_de', '5/1/20 '],
           ['pd_amnt', '$200.00 ']],
           [['pd', 1],
           ['pd_de', None],
           ['pd_amnt', None]],
           [['pd', 1],
           ['pd_de', '5/1/19 '],
           ['pd_amnt', '$300.00 '],
           ['pd', 2],
           ['pd_de', '5/1/20 '],
           ['pd_amnt', '$600.00 '],
           ['pd', 3],
           ['pd_de', '6/1/18'],
           ['pd_amnt', '$450.00']]]

Using this, I would like to create a list of dictionaries. I am dong the following to create a list of dictionaries,

list_dict = []

for i in my_list:
    temp_dict = {}
    for j in i:
        temp_dict[j[0]] = j[1]
    list_dict.append(temp_dict)

And I am getting an output like this, which I do not want,

[{'pd': 1, 'pd_de': None, 'pd_amnt': '$10.00'},
 {'pd': 2, 'pd_de': '5/1/20 ', 'pd_amnt': '$200.00 '},
 {'pd': 1, 'pd_de': None, 'pd_amnt': None},
 {'pd': 3, 'pd_de': '6/1/18', 'pd_amnt': '$450.00'}]

I need an output like this,

[{'pd_1': 1, 'pd_de_1': None, 'pd_amnt_1': '$10.00'},
 {'pd_1': 1, 'pd_de_1': '5/1/19', 'pd_amnt_1': '$100.00', 'pd_2': 2, 'pd_de_2': '5/1/20 ', 'pd_amnt_2': '$200.00 '},
 {'pd_1': 1, 'pd_de_1': None, 'pd_amnt_1': None},
 {'pd_1': 1, 'pd_de_1': '5/1/19', 'pd_amnt_1': '$300.00','pd_2': 2, 'pd_de_2': '5/1/20', 'pd_amnt': '$600.00','pd_3': 1, 'pd_de_3': '6/1/18', 'pd_amnt_3': '$450.00'}]

If you see above, they are okay when the list inside has a length of 3. If it is more than 3, then it does not give the correct result.

I am also not sure how to create "_" in keys (i.e. 'pd_1') when I create keys for dictionary.

How can I achieve my desired output?

(Note: Not sure how to name the title, I said length of list, I might be wrong there because I am not familiar with pythonic terms)

How do you convert two lists into a dictionary?, Note that the restriction with keys in Python dictionary is only immutable data types can be used as keys, which means we cannot use a dictionary of list as a key . If L1 and L2 are list objects containing keys and respective values, following methods can be used to construct dictionary object. Zip two lists and convert to dictionary using dict() function

Retaining the order of items:

import pandas as pd
from collections import OrderedDict

# my_list = ...

res = []
for l1 in my_list:
    d = OrderedDict()
    for l2 in l1:
        if l2[0] == 'pd':
            sfx = l2[1]
        d[f'{l2[0]}_{sfx}'] = l2[1].strip() if isinstance(l2[1], str) else l2[1]
    res.append(d)

df = pd.DataFrame(res)
print(df)

The output:

   pd_1 pd_de_1 pd_amnt_1  pd_2 pd_de_2 pd_amnt_2  pd_3 pd_de_3 pd_amnt_3
0     1    None    $10.00   NaN     NaN       NaN   NaN     NaN       NaN
1     1  5/1/19   $100.00   2.0  5/1/20   $200.00   NaN     NaN       NaN
2     1    None      None   NaN     NaN       NaN   NaN     NaN       NaN
3     1  5/1/19   $300.00   2.0  5/1/20   $600.00   3.0  6/1/18   $450.00

How to add a list to a Python dictionary, For this, simply declare a dictionary, and then run nested loop for both the lists and assign key and value pairs to from list values to dictionary. filter_none. edit Creating a list of all keys in dictionary using dict.keys () In python, dictionary class provides a member function i.e. dict.keys() It returns a view object or iterator to the list of all keys in dictionary. We can use this object for iteration or creating new list.

You can use additional variable (counter) to find key "index" which doesn't exist in dictionary yet:

result = []
for sub_list in my_list:
    temp = {}
    for key, value in sub_list:
        counter = 1
        while f"{key}_{counter}" in temp:
            counter  += 1
        temp[f"{key}_{counter}"] = value
    result.append(temp)

A bit more efficient solution will be to store counters into dict and increment them once key used:

result = []
for sub_list in my_list:
    counters = {}
    temp = {}
    for key, value in sub_list:
        if key in counters:
            counters[key] += 1
        else:
            counters[key] = 1
        temp[f"{key}_{counters[key]}" ] = value
    result.append(temp)

Using collections.defaultdict you can write it a bit shorter:

from collections import defaultdict

result = []
for sub_list in my_list:
    counters = defaultdict(int)
    temp = {}
    for key, value in sub_list:
        counters[key] += 1
        temp[f"{key}_{counters[key]}"] = value
    result.append(temp)

Python Dictionary, All of the compound data types we have studied in detail so far — strings, lists, They are Python's built-in mapping type. Another way to create a dictionary is to provide a list of key:value pairs using the same syntax as the previous output:. Python | Filter dictionary key based on the values in selective list In Python, sometimes we require to get only some of the dictionary keys and not all. This problem is quite common in web development that we require to get only the selective dictionary keys from some given list.

The reason you are getting this is because when you set a key in a dictionary to something, it will override any previous data. For example, you have this dictionary x = ["a":1, "b":2, "c":3] if you do x["d"] = 4 it will then be ["a":1, "b":2, "c":3, "d":4] but if you then do x["a"] = 3 it will be ["a":3, "b":2, "c":3, "d":4]. The solution for you is to add each item into the dictionary with a number after the tag to represent which tag it is.

list_dict = []

for i in my_list:
    temp_dict = {}
    for j in i:
        a = 1
        while j[0]+"_"+str(a) in temp_dict:
            a += 1
        temp_dict[j[0]+"_"+str(a)] = j[1]
    list_dict.append(temp_dict)

Python, Let's create a dictionary from this list with list elements as keys and values We have passed two lists objects in zip() , so it will return a list of tuples, If length of keys list is less than list of values then remaining elements in  this function creates a dictionary from given two lists, it prepares a dict by considering the length of the lists. it prepares the dict only to the items present in the 1st list 'a'. If length of list 'b' is less than the length of list 'a', then it appends 'None' to 'b' to make its length equal to the length of list 'a'.

Python, The zip built in takes two lists and creates a list of pairs from them. need to pad the list of values to the same length as the list of keys, otherwise the keys with  Varun June 9, 2018 Python : How to Sort a Dictionary by key or Value ? 2019-10-19T13:45:59+05:30 dictionary, Python No Comment In this article we will discuss how to sort the contents of dictionary by key or value.

20. Dictionaries, Many useful collections are built-in types in Python, and we will encounter them A list will change size dynamically when we add or remove elements – we Create a new list c which combines the numbers from both lists (order is unimportant). To define a dictionary literal, we put a comma-separated list of key-value  Your current approach {len(i): i for i in combination} actually picks the last tuple of a specific length and adds it to the dictionary, which is not what you want, instead you want a list of all such tuples. You can use collections.defaultdict to create a dict of lists as values.

Python : How to convert a list to dictionary ? – thispointer.com, Return zero-based index in the list of the first item whose value is equal to x. Raises a List comprehensions provide a concise way to create lists. Consider the following example of a 3x4 matrix implemented as a list of 3 lists of length 4: Performing list(d) on a dictionary returns a list of all the keys used in the dictionary,  Approach #4 : Unpacking with * Unpacking with * works with any object that is iterable and, since dictionaries return their keys when iterated through, you can easily create a list by using it within a list literal.

Comments
  • @RomanPerekhrest it is edited now. Sorry!
  • Note that dict is unordered structure - keys may not be ordered by "groupes". We could have 'pd_1': 1, 'pd_2': 2, 'pd_3': 3, 'pd_amnt_1': '$300.00 ', ... - is that acceptable ?
  • @RomanPerekhrest My ultimate goal is to create a pandas dataframe after I have the list of dictionaries. I have to test it out. With all these answers I am trying to test each of them and some terms are new. Let me get back to you on that.
  • see my updated answer with pandas involved
  • this method is lightning fast! Sorry, I really have to give correct answer to @Poojan since I did not mention about data frame in the post. You solution is elegant to create a data frame. Honestly, don't know how to handle these situations in SO.
  • your first solution gives correct results, your second solution and third solutions are giving wrong results. It keeps incrementing "_<countervalue>". Not sure if I can upvote yet.
  • @user9431057, sorry, wrote it without IDE.. Solution is to put counters initialization incide loop, edited.
  • This one is so time consuming. :(