populating a list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: populating a list (/thread-34066.html) |
populating a list - rwahdan - Jun-23-2021 Hi, I have a list items and I have some other lists that is done through coding. I need later to check if there are any items in the other lists then add them to the original items list. items_laptops = pd.DataFrame([ ["Item Name(Brand)"," Price(AED/Unit)"], ["Acer Aspire 3", "1698"], ["Asus Vivobook 15", "1999"], ["HP Pavilion x360", "2479"], ["Dell XPS 13", "3499"], ["HP ProBook 450", "3849"], ["HP EliteBook 840", "4299"], ["Dell XPS Ultrabook", "5399"] ]) items = [["Item Type", "Item Name", "Price AED"]] len_laptops = len(laptops) # after checking which one to buy from tkinter checkboxes if len_laptops > 0: for i in range(len(laptops)): items.append(laptops[i])so I have other list called laptops, I need to check if there are items inside the list then if yes I need to add them to the items list with the same format - each list has 3 values as it shows in the items list - ("Item Type", "Item Name", "Price AED") so for the laptop list it might have ["Laptops", "HP","3000"] so i need to take them as 3 entries in items list. so an example of the items list now should look like this items = [["Item Type", "Item Name", "Price AED"], ["Laptops", "HP","3000"] ]Note: using the above code give me an error: Thanks
RE: populating a list - rwahdan - Jun-23-2021 (Jun-23-2021, 05:46 AM)rwahdan Wrote: Hi, I found this but still having a problem: items = [ [" Item Type "," Item Name "," Item Price "] ] pre_items = [] if len_laptops > 0: for i in range(len_laptops): pre_items.append(laptops[i])in the pre_items list I am getting the values but one item each time but I need 3 items at a time. RE: populating a list - snippsat - Jun-23-2021 You seems to misunderstand how this work when you have made a Pandas Dataframe. You can not add list of 3 item when DataFrame has two columns. When working with Pandas so is normal looping as done in Python rare and not ideal. In Pandas this is called vectorization instead of looping. Guide to Pandas Quote:Unlike the approaches we will discuss below, crude looping in Pandas does not take advantage of any built-in optimizations, To run you Dateframe,this very far from a normal Python list can not just loop and add stuff(has to do it with Pandas tools). import pandas as pd items_laptops = pd.DataFrame([ ["Item Name(Brand)"," Price(AED/Unit)"], ["Acer Aspire 3", "1698"], ["Asus Vivobook 15", "1999"], ["HP Pavilion x360", "2479"], ["Dell XPS 13", "3499"], ["HP ProBook 450", "3849"], ["HP EliteBook 840", "4299"], ["Dell XPS Ultrabook", "5399"] ]) >>> items_laptops 0 1 0 Item Name(Brand) Price(AED/Unit) 1 Acer Aspire 3 1698 2 Asus Vivobook 15 1999 3 HP Pavilion x360 2479 4 Dell XPS 13 3499 5 HP ProBook 450 3849 6 HP EliteBook 840 4299 7 Dell XPS Ultrabook 5399So only two coloum,should be 3 as you last Thread ? |