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!
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']))
I am trying to replace a value in a list tuple in a list.
The index number of the list tuple corresponds to the list number.
list_tuple = [('BHS', '2018-05-24_12:14:51.985'), ('CLS', '2018-05-24_12:09:41.921'), ('CSR', '2018-05-24_12:03:15.145'), ('DRR', '2018-05-24_12:14:12.054'), ('EOF', '2018-05-24_12:17:58.810')]
list_values = [153.179, 112.829, 112.95, 287.987, 546.972]
example = [('BHS', 153.179), ('CLS', 112.829), ('CSR', 112.95), ('DRR', 287.987), ('EOF', 546.972)]
The fact that you want to combine ingredients that are at the same index means that you want to iterate over the two lists in parallel. Python's zip function is defined for such parallel iteration:
example = [(t[0],v) for t,v in zip(list_tuple,list_values)]
produces the list that you are trying to construct.
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']]
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
I have a list of lists containing information about smartphone applications. Each list (within the list) contains the same type of information, in the same order.
[id, name, ..., ].
The list of lists looks like this: [[id1, name1,...], [id2, name2, ...]]
I want to access the 10th index in each list and check its value.
I tried this, but it does not work. I imagined this would iterate over every list, except the first which is a header, and would select the 10th item in each list.
for c_rating in apps_data[1:][10]:
print(c_rating)
Instead, it prints out every item within the 10th list.
The given solution is:
for row in apps_data[1:]:
c_rating = row[10]
print(c_rating)
I understand why this code works. It breaks the process into two steps. I don't understand why the first code does not work. Any help would be appreciated.
That's due to the python expression evaluation order.
apps_data[1:][10] is evaluated in this order:
apps_data[1:] -> this gives a list of the inner lists with all but the first inner list. Let's call this inner_lists
inner_lists[10] -> this gives you the 10th element from that list of lists. Which gives you one of those inner lists.
So you end up with a select + select
What you want is a iterate + select. You can do it like this:
[print(x[10]) for x in apps_data]
This goes through all the inner_lists, selecting the 10th element from each and prints it.