TransWikia.com

Python Iterating through List of List

Stack Overflow Asked by JaydaMan on November 18, 2021

Heres my code

stockList = [
    ['AMD', '57.00', '56.23', '58.40', '56.51'],
    ['AMZN', '3,138.29', '3,111.03', '3242.56689', '3,126.58'],
    ['ATVI', '80.76', '79.16', '81.86', '79.55'],
    ['BA', '178.63', '168.86', '176.96', '169.70'],
    ['BAC', '24.42', '23.43', '23.95', '23.54'],
    ['DAL', '26.43', '25.53', '26.87', '25.66'],
    ['FB', '241.75', '240.00', '248.06', '241.20'],
    ['GE', '7.04', '6.76', '6.95', '6.79'],
    ['GOOGL', '1,555.92', '1,536.36', '1,576.03', '1,544.04'],
    ['GPS', '12.77', '12.04', '12.72', '12.10'],
    ['GRUB', '70.96', '69.71', '70.65', '70.06'],
    ['HD', '262.42', '258.72', '261.81', '260.01'],
    ['LUV', '33.62', '32.45', '33.53', '32.61'],
    ['MSFT', '208.75', '206.72', '213.58', '207.76'],
    ['MU', '51.52', '50.49', '52.31', '50.74'],
    ['NFLX', '490.10', '492.26', '511.52', '494.72', 'SUCCESS'],
    ['PCG', '9.49', '8.96', '9.52', '9.01'],
    ['PFE', '36.69', '35.87', '37.02', '36.05'],
    ['QQQ', '264.00', '263.27', '267.11', '264.58', 'SUCCESS'],
    ['ROKU', '153.36', '148.37', '153.70', '149.11'],
    ['SHOP', '952.83', '976.45', '1,036.25', '981.33', 'SUCCESS'],
    ['SPY', '325.01', '323.64', '325.47', '325.25', 'SUCCESS'],
    ['SQ', '126.99', '125.13', '130.80', '125.76'],
    ['T', '30.25', '29.58', '30.07', '29.73'],
    ['TSLA', '1,568.36', '1,646.56', '1,712.58', '1,654.79', 'SUCCESS'],
    ['TTWO', '153.06', '152.45', '154.47', '153.22', 'SUCCESS'],
    ['TWTR', '37.01', '36.03246', '36.7210083', '36.21'],
    ['WFC', '26.20', '24.45272', '25.0438213', '24.57'],
    ['WMT', '132.33', '130.8515', '132.522049', '131.51']
]

keyword = 'SUCCESS'
secondList = []
for item in stockList:
    if item[4] == keyword:
        secondList.append(stockList[0])
print(secondList)

My use case is, to go through this lists of list, find which list contains the keyword, from there send the first item in the list. I am able to get it with one single list, however I can’t do it with a list of list.

On top of that, how would I go through a dictionary containing lists?

{
    'majorDimension': 'ROWS',
    'range': 'Sheet1!A2:F30',
    'values': [
        ['AMD', '57.00', '56.23', '58.40', '56.51'],
        ['AMZN', '3,138.29', '3,111.03', '3242.56689', '3,126.58'],
        ['ATVI', '80.76', '79.16', '81.86', '79.55'],
        ['BA', '178.63', '168.86', '176.96', '169.70'],
        ['BAC', '24.42', '23.43', '23.95', '23.54'],
        ['DAL', '26.43', '25.53', '26.87', '25.66'],
        ['FB', '241.75', '240.00', '248.06', '241.20'],
        ['GE', '7.04', '6.76', '6.95', '6.79'],
        ['GOOGL', '1,555.92', '1,536.36', '1,576.03', '1,544.04'],
        ['GPS', '12.77', '12.04', '12.72', '12.10'],
        ['GRUB', '70.96', '69.71', '70.65', '70.06'],
        ['HD', '262.42', '258.72', '261.81', '260.01'],
        ['LUV', '33.62', '32.45', '33.53', '32.61'],
        ['MSFT', '208.75', '206.72', '213.58', '207.76'],
        ['MU', '51.52', '50.49', '52.31', '50.74'],
        ['NFLX', '490.10', '492.26', '511.52', '494.72', 'SUCCESS'],
        ['PCG', '9.49', '8.96', '9.52', '9.01'],
        ['PFE', '36.69', '35.87', '37.02', '36.05'],
        ['QQQ', '264.00', '263.27', '267.11', '264.58', 'SUCCESS'],
        ['ROKU', '153.36', '148.37', '153.70', '149.11'],
        ['SHOP', '952.83', '976.45', '1,036.25', '981.33', 'SUCCESS'],
        ['SPY', '325.01', '323.64', '325.47', '325.25', 'SUCCESS'],
        ['SQ', '126.99', '125.13', '130.80', '125.76'],
        ['T', '30.25', '29.58', '30.07', '29.73'],
        ['TSLA', '1,568.36', '1,646.56', '1,712.58', '1,654.79', 'SUCCESS'],
        ['TTWO', '153.06', '152.45', '154.47', '153.22', 'SUCCESS'],
        ['TWTR', '37.01', '36.03246', '36.7210083', '36.21'],
        ['WFC', '26.20', '24.45272', '25.0438213', '24.57'],
        ['WMT', '132.33', '130.8515', '132.522049', '131.51'],
    ]
}

3 Answers

Based upon your question understanding. Your question is divided into two parts, these are:

  1. How to iterate over list of lists, and get the first item from the nested list, and store it in another list
  2. How to iterate over dictionary item, to perform the same operation

If my understanding is right, then you might want to check this out.

Please note: I have not used variable keyword, simply used "SUCCESS", just replace keyword with "SUCCESS" in the code, and you are good to go.

1. FIRST SOLUTION

# to get nested list
for item in stockList:
    # this checks whether SUCCESS is present inside a list
    # python way of doing it
    if "SUCCESS" in item: secondList.append(item[0])
    
print(secondList)

# OUTPUT 
# >>> ['NFLX', 'QQQ', 'SHOP', 'SPY', 'TSLA', 'TTWO']

OR

You can do this in more pythonic way, that is to use List Comprehension

# single line approach, getting the same result
secondList = [item[0] for item in stockList if "SUCCESS" in item]
    
print(secondList)

# OUTPUT
# >>> ['NFLX', 'QQQ', 'SHOP', 'SPY', 'TSLA', 'TTWO']

2. SECOND SOLUTION

In order to get the result, first you need to assign the Dictionary to your variable, in my case, I have assigned to a variable called stockListDictionary

secondList = []        
# to get a value from key specifically
# likt any dictionary key dictionary["key_name"]
for item in stockListDictionary["values"]:
    if "SUCCESS" in item: secondList.append(item[0])

print(secondList)

# OUTPUT
# >>> ['NFLX', 'QQQ', 'SHOP', 'SPY', 'TSLA', 'TTWO']

OR

Using List Comprehension

secondList = [item[0] for item in stockListDictionary["values"] if "SUCCESS" in item]

print(secondList)

# OUTPUT
# >>> ['NFLX', 'QQQ', 'SHOP', 'SPY', 'TSLA', 'TTWO']

Answered by Alok on November 18, 2021

List comprehension makes this pretty simple. Try the following:

keyword = "SUCCESS"

# PEP8 calls for lower_underscore_case here
second_list = [i[0] for i in stockList if keyword in i]

print(second_list)

For the proposed dictionary structure, you'd just access the key containing the list, since not every value in that dict is a list:

second_list = [i[0] for i in stockList["values"] if keyword in i]

Answered by Sam Morgan on November 18, 2021

What about something like this?

keywords={"SUCCESS"}
d = # the dictionary
second_list = list()
for nested_lists in d["values"]:
    for stock_info in nested_lists:
        stock_ticker = stock_info[0]
        if stock_ticker in keywords:
            info = set(stock_info[1:])
            if info & keywords:
                second_list.append(stock_ticker)
     

Is this better? It should allow you to have more than one keyword.

Answered by O.rka on November 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP