How do i extract inner dictionary from outer list - python

I need to extract the inner dictionary from outer list ie remove the outer square brackets of the list.
example:
myList =[{'a':'1','b':'2','c':'3'},{'d':'4','e':'5'}]
Desire output:
{'a':'1','b':'2','c':'3'},{'d':'4','e':'5'}
Please note that inner dictionaries can be of dynamic size.
Any help would be great.

Just accessing the list element by index.
myList =[{'a':'1','b':'2','c':'3'},{'d':'4','e':'5'}]
d = myList[0]
So if you have k dictionaries in the list you need to access all of them, but this will be tedious.

Iterate through the list like any other list iteration:
for i in myList:
i # i is your dictionary

Related

How to filter the first element of the nested list

I am trying to filter the nested list by their first element.
[[7000, '2009-09-23'],
[200000', '2019-09-23'],
['100', '2004-01-30']]
I need to have as the output the nested list with a condition that the first element is lower than 10000.
[[7000, '2009-09-23'],
['100', '2004-01-30']]
I have tried to write this code:
filtered = [x[0][0] for x[0][0] in newList if x[0][0]<10000]
But I dont understand the concept of looping through the indexing of the nested list.
Thank you in advance
If your list is called outer then you can look at each element inner (which is in itself a list) and decide, based on the first value of inner, if you want to add it to the filtered list. This would look something like this:
filtered = [inner for inner in outer if inner[0] < 10000]

how can i add a empty list to each element of list

I have list like mylist=['a','b','c',1,2,3]. iterating through each element of list, I wanna update this list as mylist=[['a',[]],['b',[]],['c',[]],[1,[]],[2,[]],[3,[]]]. the important thing is that each list associated with elements of mylist should not point to the same list. i.e., mylist[0][1] and mylist[1][1] should not point to the same list. So that i can modify each sub list independently. Hope my question is clear. Thanks in advance
This can be achieved easily using a list comprehension along your original myList. By adding an empty list on each iteration, there is no problem with sharing a reference to the same list, which you seem to be concerned about.
myList = [[element, []] for element in myList]

insert item from an ordered list to a tuple according to index

mylist=[[('The','d'),('apple','n'),('is','v'),('red','a')],[('I','p'),('feel','v'),('fine','adv')]]
This is a list of lists of tuples, and I wish to create a new list of tuples with information from another list added to it accordingly.
new_list=[['b','i','o','o'],['o','o','o']]
for each sub_list of these two lists, I have the exact same number of items as illustrated, and I wish to add the first string of the first list in new_list to the first tuple of the first list in my_list, the second string of the first list in new_list to second tuple of first list in my_list, and so on.
The ideal output looks like this
output=[[('The','d','b'),('apple','n','i'),('is','v','o'),('red','a','o')],[('I','p','o'),('feel','v','o'),('fine','adv','o')]]
I'd appreciate any suggestions, thank you!!
Basically this question has been answered elsewhere, so I flagged the question as duplicate, but assuming that you are new to python, here is a solution to your problem.
output = [[(*t,c) for t,c in zip(l1,l2)] for l1,l2 in zip(mylist,new_list)]
The new output is created using a list nested list comprehension (creating a list of lists). To match corresponding items from two lists together, you can use zip, which returns a generator of tuples of these corresponding items. The for loop that iterates through these tuples unpacks them into two separate items, i.e.
for l1,l2 in zip(mylist,newlist)
groups the sublists of mylist and newlist together, which the for are then unpacked into the lists l1 and l2 during the for iteration. Finally, as the items of the sublists of my_list are tuples, I unpack these tuples 'on the fly' while generating the new tuple: (*t,c) is the same as (t[0],t[1],c).
Please ask if anything stayed unclear.
Hope this helps.

Outputting dictionary results for all indexes

I'm trying to output some keys, for all indexes, e.g:
print results["result"][0]["name"]
prints out the first key with no problems, and so does [1], [2], etc. I want to be able to print all indexes, for the value "name". I'm sure it uses a for loop, and tried several methods, but have failed.
How can I print the results for all indexes, not just 1?
Assuming results['result'] is a list, use a for loop to iterate over the items in that list:
for item in results['result']:
print(item['name'])
So assuming results is a dictionary-like object, results["result"] is a list containing dictionary-like elements, where these elements have a key "name",
You could use a list comprehension:
print([e["name"] for e in results["result"])
If results["result"] is a dict, this would be:
print([e["name"] for e in results["result"].values())
# or, less efficiently
print([results["result"][e]["name"] for e in results["result"])
Just join the list comprehension with newlines:
print '\n'.join(i["name"] for i in results["result"])

Uniquifying a list of lists in python

Up until now I have been using this code to uniquify (remove duplicates) from list in python:
my_list = list(set(my_list))
I now have a list of lists, I want to be able to remove duplicates from within the list of lists. For example:
(['possible-duplicate', 'random-data'], ['possible-duplicate', 'random-data'], ['possible-duplicate', 'random-data'])
I want to remove the whole sublist if possible-duplicate is a duplicate.
Can this be done?
Thanks
seen = set()
[sublist for sublist in my_list if sublist[0] not in seen and not seen.add(sublist[0])]
This happens to preserve order as well, which list(set(...)) does not.
Make a dictionary from your data:
data = (['possible-duplicate', '12345'],
['not-a-duplicate', '54321'],
['possible-duplicate', '51423'])
data_unique = dict(data)
Result is {'not-a-duplicate': '54321', 'possible-duplicate': '51423'}, or if you prefer a list of tuples, use date_unique.items(), which gives you [('not-a-duplicate', '54321'), ('possible-duplicate', '51423')].
Or for the more general case, where the sublists have more than two elements, you can use this
data_unique = dict((d[0], d) for d in data)
and then use data_unique.values() to reclaim the "uniquified" list.

Categories

Resources