How to get dictionary value from key in a list? - python

I am trying to create a new list from a list of dictionary items.
Below is an example of 1 dictionary item.
{'id': 'bitcoin',
'symbol': 'btc',
'name': 'Bitcoin',
'current_price': 11907.43,
'market_cap': 220817187069,
'market_cap_rank': 1}
I want the list to just be of the id item. So what I am trying to achieve is a list with items {'bitcoin', 'etc', 'etc}

You can use list comprehension:
my_list = [{'id': 'bitcoin', 'symbol': 'btc', ...}, ...]
[d['id'] for d in my_list]
Which translates to : for each dictionary in my_list, extract the 'id' key.

id_list = [d["id"] for d in dictlist ]
This should work for you

list = [ i['id'] for i in list_of_dict]
this should help

Simple and readable code that solves the purpose:
main_list = []
for item in main_dict:
main_list.append(item.get("id", None))
print(main_list)

Related

Create many lists from list of dictionaries that have different structures

I have a big list of dictionaries. And the dictionaries don´t have the same structure. But, I don´t know all the structures (my list has 1 million elements).
For example:
mylist = [{'name': 'Juan Carlos','age':38},{'name':'David','country':'Brazil'},
{'name':'Agustina', 'country': 'Argentina'},{'name': 'Renzo','age':24}]
I want to separate mylist into different lists, and each list has dictionaries with the same keys.
For example:
list1 = [{'name': 'Juan Carlos','age':38},{'name': 'Renzo','age':24}]
list2 = [{'name':'David','country':'Brazil'},{'name':'Agustina', 'country': 'Argentina'}]
The problem is I don´t know how many sublists I'm going to have.
To do this, you need to create a dictionary where the key is the set of keys that are present:
mylist = [{'name': 'Juan Carlos','age':38},{'name':'David','country':'Brazil'},{'name':'Agustina', 'country': 'Argentina'},{'name': 'Renzo','age':24}]
sublists = {}
for sl in mylist:
keys = tuple(set(sl.keys()))
if keys not in sublists:
sublists[keys] = []
sublists[keys].append( sl )
print( sublists )
Output:
{('name', 'age'): [{'name': 'Juan Carlos', 'age': 38}, {'name': 'Renzo', 'age': 24}], ('name', 'country'): [{'name': 'David', 'country': 'Brazil'}, {'name': 'Agustina', 'country': 'Argentina'}]}
You can use a frozenset of keys as a new key:
mylist = [{'name': 'Juan Carlos','age':38},{'name':'David','country':'Brazil'},
{'name':'Agustina', 'country': 'Argentina'},{'name': 'Renzo','age':24}]
sep = {}
for d in mylist:
sep.setdefault(frozenset(d), []).append(d)
and then you can get a list of lists:
print(sep.values())

Remove dictionary from list if it contains forbidden value

I have a list of dictionaries:
my_dicts = [
{'name': 'aaa',
'codename': 'bbbb',
'type': 'cccc',
'website': 'url1'},
{'name': 'aaa2',
'codename': 'bbbb2',
'type': 'cccc2',
'product_url': 'url2'},
{'name': 'aaa2',
'codename': 'bbbb2',
'type': 'cccc2',
'dop_url': 'url3'}
]
and a list:
my_list = ['url1', 'url3']
My goal is to have this output:
my_dicts = [
{'name': 'aaa2',
'codename': 'bbbb2',
'type': 'cccc2',
'product_url': 'url2'}
]
I want to find the most efficient way to remove the dictionaries where any value of said dictionary is in a list.
I tried this, but I'm getting the following error: RuntimeError: dictionary changed size during iteration.
for url in my_list:
for e in my_dicts:
for key, value in e.items():
if value == url:
del e[key]
You can use a list comprehension with all(), retaining only the dictionaries that do not have a key-value pair where the value appears in my_list:
[item for item in my_dict if all(url not in item.values() for url in my_list)]
This outputs:
[{'name': 'aaa2', 'codename': 'bbbb2', 'type': 'cccc2', 'product_url': 'url2'}]
One approach is to have an outer loop that loops over each element in the list my_dicts and to have an inner loop that loops over each element of a given dict. For a given iteration of the outer loop, we store the index of the dict inside my_dicts inside the indices list if one of the values in the dict is contained in my_list. After completing our scan over my_dicts, using the indices we know which elements of my_dicts to remove.
indices = []
# locate dicts with a value that is in my_list
for index, elem in enumerate(my_dicts):
for key in elem:
if elem[key] in my_list:
indices.append(index)
break
# remove dicts
for i in reversed(indices):
my_dicts.pop(i)
print(my_dicts)
Output
[{'name': 'aaa2', 'codename': 'bbbb2', 'type': 'cccc2', 'product_url': 'url2'}]
I hope this helps!

python, map name from a list to a list of dict

I have the following list and list of dicts:
data = [dict(position=1, value=150.3),
dict(position=0, value=28.5),
dict(position=2, value=1050.3)]
names = ["CL", "ES", "EUR"]
I would like to map the values of the list into the list of dicts so they match the value stated in the key "position" of the dict - to get the following result:
data = [dict(name="ES", position=1, value=150.3),
dict(name="CL", position=0, value=28.5),
dict(name="EUR", position=2, value=1050.3)]
Is there any "smart" and pythonic way to achieve that?
First of all, please present your question in actual Python form. dict is a type; the way you represent dictionaries in Python is with {}.
Also, you don't have "a dict and list", you have two lists, one of which consists of three dictionaries. So:
data = [
{'position': 1, 'value': 150.3},
{'position': 0, 'value': 28.5},
{'position': 2, 'value': 1050.3}
]
names = ["CL", "ES", "EUR"]
So, given that you do have lists, there is no concern about ordering. A simple loop will give you what you want:
for d in data:
d['name'] = names[d['position']]
This updates data in place:
>>> data
[{'position': 1, 'name': 'ES', 'value': 150.3}, {'position': 0, 'name': 'CL', 'value': 28.5}, {'position': 2, 'name': 'EUR', 'value': 1050.3}]
You can use a list comprehension and dictionary update:
data = [dict(position = 2, value=150.3),
dict(position = 1, value = 28.5),
dict(position=3, value=1050.3)]
names = ['CL', 'ES', 'EUR']
# Sort names according to "position" value of the dictionary
sorted_names = [names[idx] for idx in map(lambda x: x['position'], data)]
# Update modifies inplace
_ = [data[idx].update({'name' : el}) for idx, el in enumerate(sorted_names)]
Which gives the expected output:
data
[{'name': 'ES', 'position': 2, 'value': 150.3},
{'name': 'CL', 'position': 1, 'value': 28.5},
{'name': 'EUR', 'position': 3, 'value': 1050.3}]
You could try:
data = [{"position": 2, "value": 150},
{"position": 1, "value": 200}]
names = ["CL", "ES"]
for item in data:
item["name"] = names[item["pos"] - 1]
Where we go through all the dictionaries in the list, then for each dictionary we set the "name" key to be equal to the value in data at the position described in item["pos"] minus 1.
This of course assumes your data is clean and all items in data correctly map to items in names.
If this is not the case, use a try-except:
for item in data:
try:
item["name"] = names[item["pos"] - 1]
except IndexError:
item["name"] = None
You can also use the update method on the dictionary elements in the list, if you like the keyword-argument convention, as the style of your question suggests.
for item in data:
item.update(name=names[item["position"]])
A one liner implementation using list comprehension.
print [dict(d.items()+[('name',names[d['position']-1])]) for d in data]

Remove dictionary from list with multiple conditions

list1 = [
{'id': 1, 'country': 'Italy'},
{'id': 2, 'country': 'Spain'},
{'id': 3, 'country': 'Japan'}
]
I use this code to remove from list1 every dictionary that has country != Italy:
list2 = [element for element in list1 if element['country'] == 'Italy']
But I to include in list2 dictionaries which country == 'Italy' AND country == 'Spain' and remove all the others (or even better pop them from list1 without creating another one). How can I do this in one line=
If you really want a one-liner, you can use a list comprehension with an in-place list update:
list1[:] = [d for d in list1 if d['country'] in ('Spain', 'Italy')]

Python: Split dictionary inside a list into multiple lists

I would like to split a string like this:
my_list = [{'lat': -27.239722222222223, 'name': 'Geraldton', 'long': 114.62222222222222}]
into individual values:
my_list2 = ['lat', -27.239,'name', 'Geraldton', 'long', 114.6222]
or into dictionary or list where I can call the elements to use.
You can use dict.items to get all tuples (key, value) in the dictionary, all that is left is to flatten them:
my_list = [{'lat': -27.239722222222223, 'name': 'Geraldton', 'long': 114.62222222222222}]
dictionary = my_list[0]
# flatten using list comprehension
flattened = [item for tup in dictionary.items() for item in tup]
Output:
['lat', -27.239722222222223, 'long', 114.62222222222222, 'name', 'Geraldton']
You could do something like this:
list1 = [{'lat': -27.239722222222223, 'name': 'Geraldton', 'long': 114.62222222222222}]
list2 = []
for key in list1[0]:
list2.append(key)
list2.append(list1[0][key])

Categories