0

If I create lists to hold a column each for what will be a database table. How would I loop though the ID list to add one row at a time to a new object. So each object will hold one row for what will be a database table.

Example Lists

ListID[1,2,3,4,5]
List1[34,56,34,345,55]
list2[644,64,232,44,1]
list3[622,12,44,55,01]

I then want to create a function that will iterate though ListID and create a 'list object' eg, [1, 34, 644, 622] would be the first object then [2, 56, 64, 12]. I'm not sure how to preform this but keep it in a format that can still be persisted to a database table

Cheche
  • 1,456
  • 10
  • 27
  • For this particular purpose, you can use a list of lists, with the ListID being the first one, and the rest will follow. Then you can simply iterate through the lists and pick Nth element of each of them. – Nitin Pawar Nov 12 '18 at 13:10
  • Cant you use a df? – Joe Nov 12 '18 at 13:11

3 Answers3

0

If I understood correctly, you could do something like that, using zip:

ListID = [1,2,3,4,5]
List1 = [34,56,34,345,55]
list2 = [644,64,232,44,1]
list3 = [622,12,44,55,1]

objectList = [list(elem) for elem in zip(ListID,List1,list2,list3)]
print(objectList)

Output:

[[1, 34, 644, 622], [2, 56, 64, 12], [3, 34, 232, 44], [4, 345, 44, 55], [5, 55, 1, 1]]

An alternative would be to use map function, along with zip:

objectList = list(map(lambda elem: list(elem), zip(ListID,List1,list2,list3)))
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
  • Would that still be able to be persisted to a database table into separate columns as I could with separate lists for each column? –  Nov 12 '18 at 14:08
  • For persistency it is better to look into [sqlite3](https://docs.python.org/3/library/sqlite3.html) and [pickle](https://docs.python.org/3/library/pickle.html#module-pickle) (refer to [this](https://stackoverflow.com/questions/6161156/how-to-persist-data-between-executions-in-python) question). – Vasilis G. Nov 12 '18 at 15:30
0

You can use dictionary combined with zip, and this way you can also retrieve data using ListID:

ListID = [1,2,3,4,5]
List1 = [34,56,34,345,55]
List2 = [644,64,232,44,1]
List3 = [622,12,44,55,1]
dictObj = {}

for ID, l1, l2, l3 in zip(ListID, List1, List2, List3):
    dictObj[f'{ID}'] = [ID, l1, l2, l3]
TheBinaryGuy
  • 285
  • 4
  • 15
  • So even if the ListID started at 45, would the dictionary key start at 45 also? Could I still persist a dictionary to a database table into separate columns as I could with separate lists for each column? –  Nov 12 '18 at 14:05
  • @Liights Yup, the keys of dictionary will match your ListID. And you should be easily able to get values from the dict and save them to database. – TheBinaryGuy Nov 12 '18 at 14:10
0

If you don't want to use any third-party libraries like pandas, you can use defaultdict for an easy implementation:

from collections import defaultdict

dd = defaultdict(list)

listID = [1,2,3,4,5]
list1 = [34,56,34,345,55]
list2 = [644,64,232,44,1]
list3 = [622,12,44,55,1]

for idx, c1, c2 in zip(listID,list1, list2):
    dd[idx].extend([c1, c2])

dd
# defaultdict(list,
#            {1: [34, 644],
#             2: [56, 64],
#             3: [34, 232],
#             4: [345, 44],
#             5: [55, 1]})


for idx, item in zip(listID,list3):
    dd[idx].append(item)

dd
# defaultdict(list,
#            {1: [34, 644, 622],
#             2: [56, 64, 12],
#             3: [34, 232, 44],
#             4: [345, 44, 55],
#             5: [55, 1, 1]})
Dani G
  • 1,202
  • 9
  • 16