I have a list of strings represented as dictionaries and I want to convert them to dictionaries using list comprehension. my data looks like this:
['{"A":"1"}','{"B":"2"}']
The output should be:
[{"A":"1"},{"B":"2"}]
I tried this:
dict_var = [i for i in list_var]
And it's just returning the same result without any change!
This can be achieved by using json.loads(i) and in a list comprehension such as:
dict_var = [json.loads(i) for i in list_var]
Related
If I have the following list of ids:
id_numbers = ['08a592a1-5a40-46b7-b9b3-b3a1e6bfdb9f', 'a31017f7-13b4-401c-81ca-fd57042bc181']
I can add these id numbers as values of a defined key "id_numbers" in a new dictionary using the following:
catalogue_ids = {"id_numbers": []}
for item in id_numbers:
catalogue_ids["id_numbers"].append(item)
I was wondering if there is a dictionary comprehension way of doing the same thing?
catalogue_ids = {"id_numbers": [item for item in id_numbers]}
There's no need to use list/dictionary comprehensions. You can simply assign the id_numbers variable to the dictionary element.
catalogue_ids = {"id_numbers": id_numbers}
I'm trying to get a single key value pair using dictionary comprehension as an exercise, I have accomplished this using for loops but the best I can do using dictionary comprehension returns an entire dictionary. If I use anything other than 'inner_key01' or 'inner_key02' in the if portion of the below code I get an empty dictionary.
I would like the code to return 'inner_value22'
my_dict = {'inner_key01' :{'inner_key1': 'inner_value1', 'inner_key2': 'inner_value2'},
'inner_key02' :{'inner_key21': 'inner_value21', 'inner_key22': 'inner_value22'}
}
next_dict = {inner_key: inner_value for inner_key, inner_value in my_dict.items() for outer_key, outer_value in my_dict.items()if inner_key == 'inner_key02'}
print(next_dict)
I have a list called rest which contains many dictionaries in a list which is in the format
rest = [{'a':'b','c':'d','e':'f'}, {'a':'g','c':'h','e':'i}, {'a':'j','c':'k','e':'l'}]
Can I get an output as below where I have new as a key inside a dictionary for all the key-value pair except the first key-value pair
output = [{'a':'b','new':{'c':'d','e':'f'}},{'a':'g','new':{'c':'h','e':'i'}},{'a':'j','new':{'c':'k','e':'l'}}]
Is it possible?
You can use the syntax first, *remainder to extract the relevant parts and then create a new dict from them:
def convert(d):
first, *remainder = d.items()
return dict([first, ('new', dict(remainder))])
Then to convert each of the dicts:
output = [convert(d) for d in rest]
Note that this syntax was introduced in Python 3.0 and dictionaries are unordered before Python 3.6 (i.e. the first item is not determined).
I have some coordinate data that is lng,lat however I need it lat,lng
I have them in a list of tuples and need to switch them around:
myList = [(-87.93897686650001, 41.8493707892),
(-87.93893322819997, 41.8471652588),
(-87.9292710931, 41.8474548975),
(-87.91960917239999, 41.8477438951),
(-87.91927828050001, 41.8404034535)]
I have tried
newList = []
for i in range(len(myList)):
for c, x in reversed(list(enumerate(myList[i]))):
newList.append(x)
I get the below output, which is close
[41.8493707892,
-87.93897686650001,
41.8471652588,
-87.93893322819997,
41.8474548975,
-87.9292710931,
41.8477438951,
-87.91960917239999,
41.8404034535,
-87.91927828050001]
But I am looking for
[(41.8493707892, -87.93897686650001),
(41.8471652588, -87.93893322819997),
(41.8474548975, -87.9292710931),
(41.8477438951, -87.91960917239999),
(41.8404034535, -87.91927828050001)]
ideally i would like to do this all in the nested for-loop; but I do not care if I have to do something with the newList to group the pairs.
As always any help is appreciated.
Unless you're really attached to the nested for-loop, it's probably easiest to use this sort of list comprehension:
newlist=[i[::-1] for i in myList]
>>> newlist
[(41.8493707892, -87.93897686650001), (41.8471652588, -87.93893322819997), (41.8474548975, -87.9292710931), (41.8477438951, -87.91960917239999), (41.8404034535, -87.91927828050001)]
You can do this very easy with list comprehension.
[(b,a) for a,b in myList]
Right, I have a dictionary like this one:
my_dict = {'BAM': (1.985, 1.919), 'PLN': (4.509, 4.361),'SEK': (9.929, 9.609), 'CZK': (27.544, 26.544),
'NOK': (9.2471, 8.9071), 'AUD': (1.4444, 1.4004),
'HUF': (315.89, 307.09), 'GBP': (0.8639, 0.8399),
'HRK': (7.6508, 7.4208), 'RUB': (71.9393, 66.5393),
'USD': (1.0748, 1.0508), 'MKD': (62.11, 60.29),
'CHF': (1.0942, 1.0602), 'JPY': (121.83, 118.03),
'BGN': (1.979, 1.925), 'RSD': (124.94, 121.14),
'DKK': (7.5521, 7.3281), 'CAD': (1.4528, 1.4048)}
I need to write a function (my_dict, ["GBP", "USD", "RUB", "HRK", "HUF"]) that returns this:
GBP......0.8639......0.8399
USD......1.0748......1.0508
RUB.....71.9393.....66.5393
HRK......7.6508......7.4208
HUF....315.8900....307.0900
We are learning how to format string, and I have no idea how to approach this. Any help would be appreciated. It needs to be formatted exactly like this, with all the dots and stuff (without the empty space before GBP).
One of the simpler ways to do what you're asking is to use a
list comprehension:
def my_function(dict, list):
return ["{0}......{1}......{2}".format(item, dict[item][0], dict[item][1])
for item in list
if item in dict]
my_function will return a list of currency......value......value items. In fact, you don't even need to create a function:
strings = ["{0}......{1}......{2}".format(item, dict[item][0], dict[item][1])
for item in list
if item in dict]
If however you don't want to use a list comprehension, the same function could look like this:
def my_function(dict, list):
strings = []
for item in list:
if item in dict:
strings.append("{0}......{1}......{2}".format(item, dict[item][0], dict[item][1]))
return strings