I have a list that looks like this:
[ 'abc=lalalla', 'appa=kdkdkdkd', 'kkakaka=oeoeoeo']
And I want to split this list by '=' so that everything on the left side will become keys and on the right, values.
{ 'abc':'lalalla', 'appa':'kdkdkdkd', 'kkakaka':'oeoeo' }
The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
Method 1: Split dictionary keys and values using inbuilt functions. Here, we will use the inbuilt function of Python that is . keys() function in Python, and . values() function in Python to get the keys and values into separate lists.
Unpack the values to split a string into multiple variables, e.g. a, b = my_str. split(' ') . The str. split() method will split the string into a list of strings, which can be assigned to variables in a single declaration.
To split a list into n parts in Python, use the numpy. array_split() function. The np. split() function splits the array into multiple sub-arrays.
a = [ 'abc=lalalla', 'appa=kdkdkdkd', 'kkakaka=oeoeoeo'] d = dict(s.split('=') for s in a) print d Output: {'kkakaka': 'oeoeoeo', 'abc': 'lalalla', 'appa': 'kdkdkdkd'}
http://codepad.org/bZ8lGuHE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With