Home > database >  How do I loop through a list and find the index of a common element in another list?
How do I loop through a list and find the index of a common element in another list?

Time:06-25

I have a long list of Tickers and Weights that look something like this.

SP500 = ['MSFT', 'AAPL', 'AMZN', 'TSLA', 'GOOGL', 'GOOG', 'FB']
weight500 = [6, 5.9, 3, 2.5, 2.3, 2.1, 2]

I also have a list of screened stock tickers.

portfolio = ['MSFT', 'GOOGL', 'GOOG']

I need to get the index's of the portfolio stocks in the SP500 list and then use that to get their corresponding weight, which I then want to put in a dataframe.

I'm to create the dataframe by having 3 lists: index's, portfolio (stock name), weight.

This is how I plan on doing that: enter image description here

I'm struggling with actually putting these lists together. Now I can get the required information of individual stocks manually like...

x = input('stock from portfolio:')
index = SP500.index('x')

print(index)        #index
print(x)            #ticker name
print(W500[index])  #weight 

#For input MSFT
>> 0
>> MSFT
>> 6

I'm struggling with doing this for each stock in the list "portfolio" and putting all of the results into the three lists. I get that I have to loop through the "portfolio" list and basically do what I've just shown you, but I can't put all the steps together.

Does anyone have any tips as to how I could go about coding this? (and maybe also briefly explain the logic behind any potential solutions you have in mind).

CodePudding user response:

You can use list.index("item") to get the index of a specific item

example:

SP500 = ['MSFT', 'AAPL', 'AMZN', 'TSLA', 'GOOGL', 'GOOG', 'FB']
weight500 = [6, 5.9, 3, 2.5, 2.3, 2.1, 2]

weight = 0
index = SP500.index("MSFT")
print(weight500(index))
>> 6

CodePudding user response:

You can use a dictionary:

SP500 = ['MSFT', 'AAPL', 'AMZN', 'TSLA', 'GOOGL', 'GOOG', 'FB']
weight500 = [6, 5.9, 3, 2.5, 2.3, 2.1, 2]
weights = {ticker: weight for ticker, weight in zip(SP500, weight)}

# If your ticker is 'GOOGL'
w = weights['GOOGL']  # w == 2.1

CodePudding user response:

something like this?:

SP500 = ['MSFT', 'AAPL', 'AMZN', 'TSLA', 'GOOGL', 'GOOG', 'FB']
weight500 = [6, 5.9, 3, 2.5, 2.3, 2.1, 2]
SP_wight_dict = dict(zip(SP500, weight500))
portfolio = ['MSFT', 'GOOGL', 'GOOG']

for i in portfolio:
    print(f"Weight = {SP_wight_dict[i]}, Index = {SP500.index(i)}")

CodePudding user response:

Instead of manually doing you can use a loop.


SP500 = ['MSFT', 'AAPL', 'AMZN', 'TSLA', 'GOOGL', 'GOOG', 'FB']
weight500 = [6, 5.9, 3, 2.5, 2.3, 2.1, 2]

x = input('stock from portfolio:')

combined_lst = zip(SP500,weight500)

for i, info in enumerate(combined_lst):
    if info[0] == x:
        print(i)
        print(x)
        print(info[1])
        break

OUTPUT

stock from portfolio:MSFT
0
MSFT
6

AND

Instead of manually putting index and weight and then converting it in a DataFrame you can do this.

import pandas as pd
SP500 = ['MSFT', 'AAPL', 'AMZN', 'TSLA', 'GOOGL', 'GOOG', 'FB']
weight500 = [6, 5.9, 3, 2.5, 2.3, 2.1, 2]

portfolio = ['MSFT', 'GOOGL', 'GOOG']


lst = []

for a in portfolio:
    index = SP500.index(a)
    weight = weight500[index]
    lst.append([index,a,weight])
df = pd.DataFrame(lst)
print(df)

OUTPUT

enter image description here

  • Related