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:
- Zip corresponding items from two sequences into anonymous object
- Group anonymous objects by value of key
- 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
Staleelementreferenceexception on Python Selenium
Read Streaming Input from Subprocess.Communicate()
Get a List of Numbers as Input from the User
How Do "And" and "Or" Act With Non-Boolean Values
Why Can't Python Parse This Json Data
Selenium.Common.Exceptions.Invalidselectorexception With "Span:Contains('String')"
Run Multiple Python Scripts Concurrently
Cross-Platform Space Remaining on Volume Using Python
How to Store the Result of an Executed Shell Command in a Variable in Python
Do Python Regular Expressions Have an Equivalent to Ruby'S Atomic Grouping
Tkinter: Attributeerror: Nonetype Object Has No Attribute ≪Attribute Name≫
How to Read a File Line-By-Line into a List
How to Add to the Pythonpath in Windows, So It Finds My Modules/Packages
How to Sort a List of Dictionaries by a Value of the Dictionary
How to Read/Process Command Line Arguments
How to Type Hint a Method With the Type of the Enclosing Class
Qtdesigner Changes Will Be Lost After Redesign User Interface