I am struggling with sublists, I would like to get items from list at indexes given in index_list where these indexes are also in sublist. I would like to also keep the structure. What is the best method to do that? Should I use zip() ?
list = [['a','b','c'],['a','b','c','d'],['a','b','c']]
index_list = [[0,2],[1,2,3],[1,2]]
For each sublist, then for each index get the corresponding value (also don't use list built-in name as variable name)
results = [[values[sub_idx][item] for item in sublist] for sub_idx, sublist in enumerate(index_list)]
CODE DEMO
Related
Objective: I have a list of 200 elements(urls) and I would like to check if each one is in a specific column of the Dataframe. If it is, I would like to remove the element from the list.
Problem: I am trying a similar solution by adding to a new list the ones that are not there but it adds all of them.
pruned = []
for element in list1:
if element not in transfer_history['Link']:
pruned.append(element)
I have also tried the solution I asked for without success. I think it's a simple thing but I can't find the key.
for element in list1:
if element in transfer_history['Link']:
list1.remove(element)
When you use in with a pandas series, you are searching the index, not the values. To get around this, convert the column to a list using transfer_history['Link'].tolist(), or better, convert it to a set.
links = set(transfer_history["Link"])
A good way to filter the list is like this:
pruned = [element for element in list1 if element not in links]
Don't remove elements from the list while iterating over it, which may have unexpected results.
Remember, your syntax for transfer_history['Link'] is the entire column itself. You need to call each item in the column using another array transfer_history['Link'][x]. Use a for loop to iterate through each item in the column.
Or a much easier way is to just check if the item is in a list made of the entire column with a one liner:
pruned = []
for element in list1:
if element not in [link for link in transfer_history['Link']]:
pruned.append(element)
If the order of the urls doesn't matter, this can be simplified a lot using sets:
list1 = list(set(list1) - set(transfer_history['Link']))
Want to insert list elements in list of list such that first element of list should be inserted to first index of first list of list then second element of list of list to first element of 2nd list of list and so on...
For eg.
lst_of_lst = [[1,2,3,4][5,6,7,8][9,10,11,12][13,14,15,16]]
list = ['a','b','c','d']
output - lst_of_lst=[['a',1,2,3,4]['b',5,6,7,8]['c',9,10,11,12]['d',13,14,15,16]]
All you need to do is just iterate over your the list you want to insert the items from and just insert the respective item at 0th position.
It can be done as follows:
for i in range(len(list)):
lst_of_lst[i].insert(0, list[i])
That's it!
Also, you missed , in defining lst_of_lst, it will give you error. And also it is not a good way to name any variable or data structure a name of data type. Like you did for list array. You can change it to _list if you want.
Little trickery for fun/speed:
lst_of_lst = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
list = ['a','b','c','d']
for L, L[:0] in zip(lst_of_lst, zip(list)):
pass
print(lst_of_lst)
Try it online!
I have a list that has been extracted from a dictionary and the first element of the new list looks like this:
The total # of elements in my list are 4200; I was wondering how would I extract the 'uri' of each individual element and place it into a new list.
Quick way using list comprehension:
[elem['nft_metadata']['data']['uri'] for elem in result['minted_nfts']]
context: I have a list of lists and I am trying to get rid of duplicate entries. The list of lists is ordered by the first element in the sublist. When the function encounters the first occurrence of the sublist, the function appends that sublist to the "non_duplicate_list". If the function encounters another sublist with the same first element it continues.
Problem: I have a function that is working perfectly, but I feel it is too long. Is there any way to shorten/make the code more concise? maybe using list comprehension?
Here is the function:
def remove_duplicates(data_set):
non_duplicate_list = []
for row in data_set:
app_name = row[0]
if any(app_name in sublist for sublist in non_duplicate_list):
continue
else:
non_duplicate_list.append(row)
I tried doing this to make the function more concise, but it didn't work:
def remove_the_dupes(data_set):
non_duplicate_list = [continue if any(row[0] not in sublist for sublist in non_duplicate_list) else non_duplicate_list.append(row) for row in data_set]
return non_duplicate_list
I then get an error message about wrong syntax
please provide any shortened version of my function if possible. Many Thanks!
this should work
not_duplicate_list = [row for row in datset if not any(row[0] in sublist for sublist in non_duplicate_list)]
you shouldn't use continue and return in a list comprehension, take a look here for more info
First of all, complete python/programming newbie here, so I apologise if this is a stupid question. Also, sorry for the awkward title.
I'm attempting to adapt the top answer in this question:
Check if a Python list item contains a string inside another string. However, I'm not sure if what I'm trying to achieve is even possible.
list6 = [list1,list2,list3,list4,list5]
'list6' is a list containing several other lists.
Then, this line can be used to check if 'Cool' is a string inside of any of those lists:
if any("Cool" in s for s in list6):
My question is: Assuming 'Cool' is only in 'list1', is it possible to fetch and store 'list1' in order to use other values within that list? For example:
if "Cool" is in any list in list6:
list = list with "Cool"
something = list[4]
You can use next with a generator expression (Thanks ShadowRanger)
next(sublist for sublist in list6 if "Cool" in sublist)
or you can use next with an iterator such as filter to get the first item that contains the element
next(filter(lambda x: "Cool" in x, list6))
You would have to use for-loop to check every sublist separatelly and then you can keep it or get other elements from this sublist.
for sublist in list6:
if "Cool" in sublist:
something = sublist[4]