potatomeister
potatomeister

Reputation: 65

find a specific item from a list of tuples

portfolio= [ 
( "Purchase Date:23-Aug-2015", "Purchase Price:43.50", "Shares:25",      'Symbol:CAT', "Current Price:92.45" ), 
( "Purchase Date:15-Mar-2013", "Purchase Price:42.80", "Shares:50",     'Symbol:DD', "Current Price:51.19" ), 
( "Purchase Date:7-Dec-2014", "Purchase Price:42.10", "Shares:75", 'Symbol:EK', "Current Price:34.87" ), 
( "Purchase Date:6-Dec-2013", "Purchase Price:37.58", "Shares:100", 'Symbol:GM', "Current Price:37.58" ) 
] 

# loop to iterate over each item in list and print each item out
for a in portfolio:
    print (a[0:5])

This is my code and I want to print out a specific item from a tuple, how do I do this?

I want to print out Purchase price from 7-Dec-2014.

Upvotes: 0

Views: 124

Answers (3)

erip
erip

Reputation: 16935

You need to check if the first item in the tuple is "Purchase Date:7-Dec-2014". If it is, you can print the second item in the tuple. Here's how to do that:

portfolio= [
( "Purchase Date:23-Aug-2015", "Purchase Price:43.50", "Shares:25",      'Symbol:CAT', "Current Price:92.45" ),
( "Purchase Date:15-Mar-2013", "Purchase Price:42.80", "Shares:50",     'Symbol:DD', "Current Price:51.19" ),
( "Purchase Date:7-Dec-2014", "Purchase Price:42.10", "Shares:75", 'Symbol:EK', "Current Price:34.87" ),
( "Purchase Date:6-Dec-2013", "Purchase Price:37.58", "Shares:100", 'Symbol:GM', "Current Price:37.58" )
]

for transaction in portfolio:
    if transaction[0] == "Purchase Date:7-Dec-2014":
        print(transaction[1])

This code is quite clunky and is neither Pythonic nor pretty. A more natural way to represent these data is with a dictionary or, more specifically, a dictionary of dictionaries. I'll use transaction dates as the key to transaction data corresponding to that date.

transactions = {
  '23-Aug-2015' : { 'Purchase Price' : 43.50, 'Shares' : 25, 'Symbol' : 'CAT', 'Current Price': 92.45 },
  '15-Mar-2013' : { 'Purchase Price' : 42.80, 'Shares' : 50, 'Symbol' : 'DD', 'Current Price': 51.19 },
  '7-Dec-2014' : { 'Purchase Price' : 42.10, 'Shares' : 75, 'Symbol' : 'EK', 'Current Price': 34.87 },
  '6-Dec-2013' : { 'Purchase Price' : 37.58, 'Shares' : 100, 'Symbol' : 'GM', 'Current Price': 37.58 }
}

Here's how you can achieve in same goal using a dictionary:

# For every key and value pair in transactions
for k, v in transactions.iteritems():
  # If we find a key with December 7th, 2014 as the date
  if k == '7-Dec-2014':
    # Print the purchase price of the transaction from that date.
    print v['Purchase Price']

This allows you to access data without needing to do any special parsing of strings.

Upvotes: 3

Joe T. Boka
Joe T. Boka

Reputation: 6589

You can iterate through portfolio and check if the Purchase date you're looking for is in any of the tuples. If so, you can print it provided the Purchase price is always in the same position in all of the tuples.

for i in portfolio:
    if "Purchase Date:7-Dec-2014" in i:
        print (i[1])

Upvotes: 2

vesche
vesche

Reputation: 1860

You'd probably be better off putting your data into dictionaries, to do cleaner look ups.

portfolio = [
{"Purchase Date":"23-Aug-2015", "Purchase Price":43.50, "Shares":25, "Symbol":"CAT", "Current Price":92.45},
{"Purchase Date":"15-Mar-2013", "Purchase Price":42.80, "Shares":50, "Symbol":"DD", "Current Price":51.19},
{"Purchase Date":"7-Dec-2014", "Purchase Price":42.10, "Shares":75, "Symbol":"EK", "Current Price":34.87},
{"Purchase Date":"6-Dec-2013", "Purchase Price":37.58, "Shares":100, "Symbol":"GM", "Current Price":37.58}
]

for data in portfolio:
    if data["Purchase Date"] == "7-Dec-2014":
        print(data["Purchase Price"])

Upvotes: 4

Related Questions