How to Make a Dictionary from Separate Lists of Keys and Values

How can I make a dictionary from separate lists of keys and values?

Like this:

keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
print(dictionary) # {'a': 1, 'b': 2, 'c': 3}

Voila :-) The pairwise dict constructor and zip function are awesomely useful.

Adding keys and values to a dictionary from two separate lists of same size

@ShadowRanger's solution

d = dict(zip(A, B))

For the second question, assuming you have a list A with length m. The for loop will run m * m times. In the last m running, it will assign 32 which is the last element in the B to every key in D.

Convert dictionary key and values into to separate lists?

You can use the dictionary keys and values methods:

my_dict={"key_a":"value_a","key_b":"value_b"}

print(list(my_dict.keys()))
print(list(my_dict.values()))

Output

['key_a', 'key_b']
['value_a', 'value_b']

How to create dict from two separate Lists - C#

It's very simple with LINQ:

keys.Zip(dates, (key,date) => new { key, date })
.GroupBy(x => x.key)
.ToDictionary(g => g.Key, g => g.Select(x => x.date).ToList())

Explanations:

  1. Zip corresponding items from two sequences into anonymous object
  2. Group anonymous objects by value of key
  3. Convert groups to dictionary

Create multiple lists and store them into a dictionary Python

I did it at the very end.

dict_stable_feature = dict()

for col in df_stable:

t1 = df[col].tolist()
t2 = df[col].unique().tolist()

for value in t2:

t = []

for i in range (0, len(t1)-1):

if value == t1[i]:

t.append(1)

else:

t.append(0)

cc = str(col)
vv = "_" + str(value)
cv = cc + vv

dict_stable_feature[cv] = t

splitting a dictionary in python into keys and values

Not that hard, try help(dict) in a console for more info :)

keys = dictionary.keys()
values = dictionary.values()

For both keys and values:

items = dictionary.items()

Which can be used to split them as well:

keys, values = zip(*dictionary.items())

Note 0 The order of all of these is consistent within the same dictionary instance. The order of dictionaries in Python versions below 3.6 is arbitrary but constant for an instance. Since Python 3.6 the order depends on the insertion order.

Note 1 In Python 2 these all return a list() of results. For Python 3 you need to manually convert them if needed: list(dictionary.keys())

How to convert each value in a dictionary to a separate list?

you need to read the data from request. your request is in form somehting like this

{
'fruits': ['list of fruits'],
'vegetables': ['list of vegetables']

}

you are reading request data in json format using data = requests.json, which is of same format as above mentioned.

now to read/save the data in list, you need to read the data from data dictionary and save it in list.

fruits = data.get('fruits', []) # [] in case no 'fruits' as key value in payload
vegetables = data.get('vegetables', [])

Here fruits and vegetables are list you save your data.



Related Topics



Leave a reply



Submit