I have two lists named queries_fetcher_list and reftable_column_name.
Now I need to perform the operation of taking an element from a and b, and make it as a tuple with zip().
This is the query for doing that:
some_list = []
for i in range (len(reftable_column_name)):
for row in queries_fetcher_list[i]:
some_list.append(dict(zip(reftable_column_name[i], row)))
Now I need that result to be append like this:
[[{first element}], [{second element}]]
What I need to do now is every time when a zip operation is performed that element has to append as a separate list, i.e. list inside a list, like this:
a = []
a = [['one'], ['two'], ['three']]
queries_fetcher_list contains a list of data that are retrived from an MySQL query retrieved with cursor.fetchall().
reftable_column_name is a list contains the column names of a table retrived with cursor.description().
some_list.append([dict(zip(reftable_column_name[i], row))])
will append single-element lists containing your dict.
define a list and everytime when elements got append into it free it, means empty it,now you can achieve what you want,eveytime when an element is appended into a list the list will be flushed and becomes an empty list but make sure to add your elements like this "list(your_element)".
Related
I have a empty list of tuple and I wish to enter values inside that tuple.
The desired output is :
lst = [()] --> lst = [(1,2,'string1','string2',3)]
A tuple is, by definition, unchangable.
You may want to replace that tuple with a new one like this:
lst = [()]
lst[0] = ("item1", "item2")
In this way you are replacing the origina tuple with a new one with the desired items. If the tuple is not empty you can do:
lst[0] = (*lst[0], "new item")
Here you are unpacking the values of the old tuple (*lst[0] is equal to "item1", "item2" in this example) and adding new items.
Note that if you are working with variable data maybe tuple is not the best data structure to use in this case.
you can't, A tuple is a collection that is ordered and immutable.
though, you can create a new tuple with the same name
What I want to do is take names from namelist, compare those to names in banklist, and if there is an item in banklist which looks a lot like an item in namelist, I want to append that item to a closematchlist. The goal of this is to find items that occur in both lists, even if there is a spelling error in namelist.
When I print(closematch), it works like intended: the close matches in banklist get found and printed. However, when I try to append those items to a list, the result of print(closematchlist) is [].
for name in namelist:
closematch = difflib.get_close_matches(name, banklist, 1, 0.8)
closematchlist = list()
closematchlist.append(closematch)
print(closematch)
print(closematchlist)```
difflib.get_close_matches() is a list of close matches. You don't need to copy it to a new list.
I'm trying to create a new empty list that will contain tuples when calling extend. Here's where I declare the list:
ticketData = list()
And I loop through another list to add tuples to this list:
data = (0, requestor, subject, thetime)
ticketData.extend(data)
When I output the result it shows this:
[0, 'Name', 'Test Subject', '03:31:12']
I need it to be a list of tuples, not just a list so that I can use sqlite's executemany function.
It seems straight forward, but I haven't been able to find an answer on here. Thanks!
Just append it to the list, so that the tuple will be added instead of having the elements of the tuple extend the list.
ticketData.append(data)
will add data to the list ticketData
Although this question has been answered already, I thought it could be useful to know about how to construct an iterable that should contain an empty iterable.
In Python, constructors like list(), set(), tuple() etc. take an iterable as input. Hence they iterate through the iterable and somthing like list(tuple()) does not return a list with an empty tuple. Instead the list constructor iterates through the tuples, and since it has no items in it it results in an empty list.
To construct a list that contains an empty tuple do the following:
Instead of my_list = list(tuple()) // returns [] or list()
Do: my_list = [tuple()]
Or: my_list = list([tuple()]) //constructor iterates through the list containing the empty tuple
Analogously, if you want to make a set containing an empty tuple do the following:
Instead of my_set = set(tuple()) // returns set()
Do: my_set = {tuple()}
Or: my_set = set([tuple()]) // constructor iterates through the list, containing the empty tuple
This also applies to other iterables. I am writing this, because I myself have had problems using these constructors to return e.g. a list containing another empty iterable.
I have a dictionary that needs its values to be 2D lists. I am having trouble adding these second values to a key. I have two lists, the first a list of values then a list of keys that is composed of additional lists.
So far I have this code:
for i in range(len(keyList)):
if keyList[i] in theInventory:
value = theInventory[keyList[i]]
value.append(valueList[i])
theInventory[keyList[i]] = value
else:
theInventory[keyList[i]] = valueList[i]
The problem is that the output has a list of entries for the first added to the list then it has the lists I am looking to add to my dictionary.
Like so:
[value, value, value, [list], [list]]
How do I make the first entry be entered into the dictionary as its own list?
Use extra [] around valueList[i]:
theInventory[keyList[i]] = [valueList[i]]
You can simplify your code as follows. The append() method mutates the list, so there is no need to assign the result of the append back the theInventory. Further there is no need to use value at all, instead you can just directly append to the item in the dictionary.
for i in range(len(keyList)):
if keyList[i] in theInventory:
theInventory[keyList[i]].append(valueList[i])
else:
theInventory[keyList[i]] = [valueList[i]]
I have a list of 100 URLs in a list called recipe_urls and I am trying to remove the the first 37 characters from each element in that list and store it in a new list called recipe_names. How can I increment the index position in .insert(). Maybe I am thinking about this wrong and there is an easier way to do this?
recipe_names = []
for url in recipe_urls:
recipe_names.insert(x, url[37:0])
This is all you need:
recipe_names = [x[37:] for x in recipe_urls]
This method of building a list from another iterable or sequence is called list comprehension
Since you always want it to go at the end of recipe_names, you can use the index -1, or even just call append instead (which doesn't take an index, and always puts the new value at the end of the list). But whenever you have code that creates an empty list, then iterates over something else to append to that new list, you can use a list comprehension instead:
recipe_names = [url[37:] for url in recipe_urls]