Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting List to Libray
#1
Dear Python Developers,
I hope you are having a great saturday. Could you please assert me if I'm coding this correct:
The goal is to convert my list into a library:

1) The code works but it provides me the same key for all items in the list. I'm I doing this right?

#Add list of stocks to dictionary
x = [("GOOGL", 125, 772.88, 941.53),
          ("MSFT", 85, 56.60, 73.04),
          ("RDS-A", 400, 49.58, 55.74),
          ("AIG", 235, 54.21, 65.27),
          ("FB", 150, 124.31, 172.45)]

mydata = {}

for i in range(len(x)):
    mydata[x[i]] = x.count(x[i])

print (mydata)
Output:
{('GOOGL', 125, 772.88, 941.53): 1, ('MSFT', 85, 56.6, 73.04): 1, ('RDS-A', 400, 49.58, 55.74): 1, ('AIG', 235, 54.21, 65.27): 1, ('FB', 150, 124.31, 172.45): 1}
Reply
#2
len probably finds the amount of tuples in the list. It then puts the tuples in the dictionary, Try using len twice to find everything inside all the tuples in the list
Reply
#3
Dear SheeppOSU,

First of all thank you for taking time out of your busy time to respond.

Could you please be more specific recoding that line.

Kind Regards,
Jehu
Reply
#4
Your terminology is incorrect. You are trying to make a dictionary, not a library. And your values are the same (1), not your keys. Keys by definition are different. If they were all the same key.

How to fix this is not clear, because what you want is not clear. Do you want the stock names a keys with the numbers as values?

mydata = {}
for stock in x:
    mydata[stock[0]] = stock[1:]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
I don't know exactly what you're trying to do but if you're trying to put each of these in a dictionary and give them the 1. This is how you would do that -
x = [("GOOGL", 125, 772.88, 941.53),
    ("MSFT", 85, 56.60, 73.04),
    ("RDS-A", 400, 49.58, 55.74),
    ("AIG", 235, 54.21, 65.27),
    ("FB", 150, 124.31, 172.45)]
mydata = {}
for i in x:
    for f in i:
        mydata.update({f : 1})

print(mydata)
Output:
{'GOOGL': 1, 125: 1, 772.88: 1, 941.53: 1, 'MSFT': 1, 85: 1, 56.6: 1, 73.04: 1, 'RDS-A': 1, 400: 1, 49.58: 1, 55.74: 1, 'AIG': 1, 235: 1, 54.21: 1, 65.27: 1, 'FB': 1, 150: 1, 124.31: 1, 172.45: 1}
Reply
#6
Thank you Ichabod801. Yes I meant dictionary, not library. Yes, I wanted the stock names as keys with the numbers as values. I really, really appreciate your help.

SheeppOSU. Thank you for your help.
Reply
#7
There is always a question what you want to do with resulting dictionary. You can do as ichabod801 suggested:

>>> x = [("GOOGL", 125, 772.88, 941.53), 
...      ("MSFT", 85, 56.60, 73.04), 
...      ("RDS-A", 400, 49.58, 55.74), 
...      ("AIG", 235, 54.21, 65.27), 
...      ("FB", 150, 124.31, 172.45)]
>>> my_data = {}
>>> for stock in x: 
...     my_data[stock[0]] = stock[1:]
...
>>> my_data
{'GOOGL': (125, 772.88, 941.53),
 'MSFT': (85, 56.6, 73.04),
 'RDS-A': (400, 49.58, 55.74),
 'AIG': (235, 54.21, 65.27),
 'FB': (150, 124.31, 172.45)}  
Now, if you want to access first value, then you must use index like this:

>>> my_data['GOOGL'][0]                                                     
125
It could become quite complicated to remember all indexes and their meaning if code is (becomes) lengthy.

One way is to use dictonary as value with descriptive names:

>>> my_data = {}
>>> headers = ('quantity', 'purchase price', 'current price')                                                            
>>> for stock in x: 
...     my_data[stock[0]] = dict(zip(headers, stock[1:])) 
...
>>> my_data                                                                 
{'GOOGL': {'quantity': 125, 'purchase price': 772.88, 'current price': 941.53},
 'MSFT': {'quantity': 85, 'purchase price': 56.6, 'current price': 73.04},
 'RDS-A': {'quantity': 400, 'purchase price': 49.58, 'current price': 55.74},
 'AIG': {'quantity': 235, 'purchase price': 54.21, 'current price': 65.27},
 'FB': {'quantity': 150, 'purchase price': 124.31, 'current price': 172.45}}
>>> my_data['GOOGL']['quantity']                                            
125
>>> sum(row['quantity'] for row in my_data.values())                         # total number of shares              
995
>>> sum(row['quantity'] * row['current price'] for row in my_data.values())  # portfolio total value
187401.6
Another possibility is to have list of dictionaries:

>>> headers = ('name', 'quantity', 'purchase price', 'current price')       
>>> my_data = []                                                            
>>> for stock in x: 
...     my_data.append(dict(zip(headers, stock))) 
...                                                                        
>>> my_data                                                                 
[{'name': 'GOOGL',
  'quantity': 125,
  'purchase price': 772.88,
  'current price': 941.53},
 {'name': 'MSFT',
  'quantity': 85,
  'purchase price': 56.6,
  'current price': 73.04},
 {'name': 'RDS-A',
  'quantity': 400,
  'purchase price': 49.58,
  'current price': 55.74},
 {'name': 'AIG',
  'quantity': 235,
  'purchase price': 54.21,
  'current price': 65.27},
 {'name': 'FB',
  'quantity': 150,
  'purchase price': 124.31,
  'current price': 172.45}]
>>> sum(row['quantity'] for row in my_data)                         # number of shares
995
>>> sum(row['quantity'] * row['current price'] for row in my_data)  # portfolio total value        
187401.6
>>> sum((row['current price'] - row['purchase price']) * row['quantity'] for row in my_data) # portfolio total profit                                                       
34762.74999999999
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting a list to dictinary tester_V 8 2,710 Jul-02-2021, 09:04 PM
Last Post: tester_V
  Converting tkinter listbox into list BigSwiggy 6 3,611 Feb-07-2021, 02:01 PM
Last Post: BigSwiggy
  Converting list to variables Palves 1 1,761 Sep-18-2020, 05:43 PM
Last Post: stullis
  Trouble with converting list , dict to int values! faryad13 7 3,751 Sep-04-2020, 06:25 AM
Last Post: faryad13
  converting list of zero length to a matrix of 3*3 vp1989 2 1,924 May-20-2020, 07:46 PM
Last Post: deanhystad
  converting string object inside a list into an intiger bwdu 4 2,605 Mar-31-2020, 10:36 AM
Last Post: buran
  more list help converting paul41 3 2,432 Nov-25-2019, 07:59 AM
Last Post: perfringo
  Converting parts of a list to int for sorting menator01 2 2,229 Nov-03-2019, 03:00 PM
Last Post: menator01
  Converting to a list and sort tantony 6 3,219 Oct-07-2019, 03:30 PM
Last Post: perfringo
  Converting List into list of tuples ARV 4 4,747 Sep-28-2019, 04:58 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020