How to remove duplicates from Python list of dictionaries? [duplicate] - python

This question already has answers here:
List of unique dictionaries
(23 answers)
how to uniqify a list of dict in python
(6 answers)
Closed 2 years ago.
I have a list of dictionaries and I would like to remove all dicts that are duplicates in number. Keep only the last!
Example:
List of dicts:
student_data = [{'number':'1234', 'url':'www.abc.com'}, {'number':'9999', 'url':'www.abc.com'}, {'number':'1234', 'url':'www.xyz.com'}]
As you can see in we have 2 dicts where number is 1234. Therefore I remove the first dict and keep the last:
List of dicts:
student_data = [{'number':'9999', 'url':'www.abc.com'}, {'number':'1234', 'url':'www.xyz.com'}]
Any idea how to do this?

Related

How to chain individual elements of list into one individual element in Python [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
I have the following list
List1 =['4','0','1','k']
How do i make sure that individual elements are combined into one single entity?
Here is the desired output
List1 =['401k']
Use str.join:
List1 = [''.join(List1)]

Iterate through a json list with dictionaries [duplicate]

This question already has an answer here:
How to loop over a list of dicts and print values of a specific key?
(1 answer)
Closed 4 years ago.
I have a json file which contains a list with many dictionaries. Here I've included just two. But I want to iterate through all of the dictionaries and only get the code value (Company1 and Company2).
[{"code":"Company1","exchange_short_name":"ST","date":"2000-01-01"},
{"code":"Company2","exchange_short_name":"ST","date":"2000-01-01"}]
I've gotten to this, but it gives me all of the values inside the dictionary, and not the code values.
for d in jsonData:
for key in d:
print(d[key])
[item['code'] for item in jsonData]
will return list of codes.

Python iterate through a list of tuples [duplicate]

This question already has answers here:
Find an element in a list of tuples
(10 answers)
How to check if all elements of a list match a condition?
(5 answers)
Closed 5 years ago.
I am new in Python and I was curious if I can do something like this:
Let's say that I have a list of tuples and one variable like this:
list = [(123,"a"),(125,"b")]
variable = (123,"c")
Is it possible to search for the first element of the variable in the list like this?
if variable[0] in list[0]:

How to get keys from the dictionary? [duplicate]

This question already has answers here:
How to return dictionary keys as a list in Python?
(13 answers)
Closed 5 years ago.
list=['Mary','Bob','Linda']
dictionary={0:'Mary', 1: 'Anna', 2:'Bob', 3:'Alice', 4: 'Linda'}
if list in set(dictionary.values()):
name = dictionary.get(None, list)
values = itemgetter(!*dic)(dictionary)
How can I get keys from the dictionary as the code find out same values from the list and dictionary?
Second question: how can I get different values keys of the dictionary between a list and a dictionary? E,g: print out the keys are [1],[3]
Thank you
I'm surprised you didn't try the obvious:
dictionary.keys()
Note: this was provided when the question explicitly asked "How to get all keys from the dictionary?".

Merge two lists,one as keys, one as values, into a dict in Python [duplicate]

This question already has answers here:
How do I combine two lists into a dictionary in Python? [duplicate]
(6 answers)
Closed 7 years ago.
Is there any build-in function in Python that merges two lists into a dict? Like:
combined_dict = {}
keys = ["key1","key2","key3"]
values = ["val1","val2","val3"]
for k,v in zip(keys,values):
combined_dict[k] = v
Where:
keys acts as the list that contains the keys.
values acts as the list that contains the values
There is a function called array_combine that achieves this effect.
Seems like this should work, though I guess it's not one single function:
dict(zip(["key1","key2","key3"], ["val1","val2","val3"]))
from here: How do I combine two lists into a dictionary in Python?

Categories