0

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().

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
pythonnoob
  • 29
  • 1
  • 3
  • 1
    give us an example data sample. what do reftable_column_name and queries_fetcher_list contain. are they always the same length? demonstrate an input list, and desired output so that we can better understand this question – konrad Feb 22 '16 at 04:18
  • As per your suggestion I have edited. – pythonnoob Feb 22 '16 at 04:24
  • perhaps this is what you are looking for? http://stackoverflow.com/questions/3748063/what-is-the-syntax-to-insert-one-list-into-another-list-in-python – Alter Feb 22 '16 at 04:37
  • No, What I want to achieve is a = [ ] for i in somerange: a.append(i) Expected output: a = [ ['first'], ['second'], ['third'] ] – pythonnoob Feb 22 '16 at 04:46
  • But `dict(zip(reftable_column_name[i], row))` is a `dict`, it doesn't have elements as a list does, only keys and values. Are your 'first', 'second' etc keys or values? – gil Feb 22 '16 at 04:57

2 Answers2

0

some_list.append([dict(zip(reftable_column_name[i], row))]) will append single-element lists containing your dict.

BingsF
  • 1,269
  • 10
  • 15
0

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)".

noobster
  • 872
  • 1
  • 6
  • 17