Skip to content
names = ['Apple Inc', 'Coca-Cola', 'Walmart']

Use the method .sort()

prices = [159.54, 37.13, 71.17]
prices.sort()
print(prices)

Use the function max()

prices = [159.54, 37.13, 71.17]
price_max = max(prices)
print(price_max)

You can use the .append() and .extend() methods to add elements to a list.

  • The .append() method increases the length of the list by one, so if you want to add only one element to the list, you can use this method.
  • The .extend() method increases the length of the list by the number of elements that are provided to the method, so if you want to add multiple elements to the list, you can use this method.
# Append a name to the list names
names.append('Amazon.com')
print(names)

# Extend list names
more_elements = ['DowDuPont', 'Alphabet Inc']
names.extend(more_elements)
print(names)
prices.extend([1705.54, 66.43, 1132.34])

Identify the index of max_price in the list prices. Use this index on the names list to identify the company with maximum stock price.

# Do not modify this
max_price = max(prices)

# Identify index of max price
max_index = prices.index(max_price)

# Identify the name of the company with max price
max_stock_name = names[max_index]

# Fill in the blanks 
print('The largest stock price is associated with ' + max_stock_name + ' and is $' + str(max_price) + '.')

Why use an array for financial analysis?

Arrays can handle very large datasets efficiently

  • Computationally-memory ecient
  • Faster calculations and analysis than lists
  • Diverse functionality (many functions in Python packages)

What's the difference?

  • Data types (np arrays can contain only a single data type)
  • Array operations ( for np arrays: element-wise sum)
import numpy as np
prices = [170.12, 93.29, 55.28, 145.3, 171.81, 59.5, 100.5]
earnings = [9.2, 5.31, 2.41, 5.91, 15.42, 2.51, 6.79]
# Create a 2D array of prices and earnings
stock_array = np.array([prices, earnings])
print(stock_array)

# Print the shape of stock_array
print(stock_array.shape)

# Print the size of stock_array
print(stock_array.size)