-1

I am new to programming and am having trouble figuring out how to loop over a list within a list using Python.

I am working with output from the U.S. Census API that is formatted as follows:

[['NAME', 'POP_2020', 'POP_2021', 'state'],
 ['Oklahoma', '3962031', '3986639', '40'],
 ['Nebraska', '1961455', '1963692', '31'],
 ['Hawaii', '1451911', '1441553', '15'],
 ['South Dakota', '887099', '895376', '46'],
 ['Tennessee', '6920119', '6975218', '47']]

I would like to write a function that will extract the state name from each of the individual lists.

Here is my code:

    import requests

    url = f'https://api.census.gov/data/2021/pep/populationget=NAME,POP_2020,POP_2021&for=state:*'
    response = requests.get(url)
    data = response.json()

    def statenames():
        for i in data:
            print(data[0])

The function above returns the following output, which isn't what I'm looking for:

['NAME', 'POP_2020', 'POP_2021', 'state']
['NAME', 'POP_2020', 'POP_2021', 'state']
['NAME', 'POP_2020', 'POP_2021', 'state']
['NAME', 'POP_2020', 'POP_2021', 'state']
['NAME', 'POP_2020', 'POP_2021', 'state']
['NAME', 'POP_2020', 'POP_2021', 'state']
['NAME', 'POP_2020', 'POP_2021', 'state']
['NAME', 'POP_2020', 'POP_2021', 'state']

I've been able to get the individual state names be writing a series of print statements like this:

print(data[0][0])
print(data[1][0])
print(data[2][0])

I know that isn't very efficient and am hoping there is a way to use a for loop instead.

Thanks in advance for your help!

pklr35
  • 7
  • 1
  • Can you please provide what you want? – Ebrahim Momin Jul 30 '23 at 03:53
  • 1
    You almost got it -- in the first code sample, you want `print(i[0])` instead of `print(data[0])`. (The way you had it, the loop variable `i` was not used, which is usually a sign that you did something wrong.) – John Gordon Jul 30 '23 at 03:56
  • The hint should be: where the code says `for i in data:`, the `i` is there for a reason, right? Is it a part of the syntax, or are you allowed to change it? If you research or check your notes, you can find that you can change it, and what its purpose is. After that, it should appear suspicious that *the rest of the code doesn't use it yet*. That's the problem: it should be doing so. If you understand what is in `i` each time through the loop, then it is obvious what to do next. – Karl Knechtel Jul 30 '23 at 05:31
  • As an aside, "sublists" aren't anything special in Python and there is not really anything separate that needs to be taught about them. In Python, a list is a thing designed to have other things in it; it **just happens** in some cases that those other things are also lists. – Karl Knechtel Jul 30 '23 at 05:40

4 Answers4

1

While looping over the whole list, get the first index from each sublist like this:

list = [['NAME', 'POP_2020', 'POP_2021', 'state'],
 ['Oklahoma', '3962031', '3986639', '40'],
 ['Nebraska', '1961455', '1963692', '31'],
 ['Hawaii', '1451911', '1441553', '15'],
 ['South Dakota', '887099', '895376', '46'],
 ['Tennessee', '6920119', '6975218', '47']]

for sublist in list:
    print(sublist[0]) # get the first item from the list

which outputs:

NAME
Oklahoma
Nebraska
Hawaii
South Dakota
Tennessee
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Thank you very much! I knew it must be something easy. The beginner classes I've been taking haven't covered sublists yet, so this was very helpful. – pklr35 Jul 30 '23 at 04:15
1

Python provides you 2 ways of iterating over data through loop. You can use the normal indexed for loop or you can do it through a for-each loop. Whichever method you want to choose, you will require a nested loop to iterate over your list. The outer loop will run for all items in your outer list, i.e., it will return the lists, as your code has been doing. The inner loop will help you iterate through a single item in that list.

Index method :

for i in range(len(data)):
    for j in range(len(data[i])):
        print(data[i][j],end=' ')
    print()

For-each Loop:

for item in data:
    for value in item:
        print(value,end=' ')
    print()

Both these codes will give the same output, so you can choose either as required. The indexed loop will be beneficial in cases where you might need to modify data based on its position. Otherwise if you only wish to go through the data, you can use for-each loop as it will reduce the risk of accidentally modifying data.

tan_000
  • 41
  • 3
0

Your for loop inside statenames was only print first item in your list. If you want to get first item of sublist inside your list. You can do this solution

list = [['NAME', 'POP_2020', 'POP_2021', 'state'],
['Oklahoma', '3962031', '3986639', '40'],
['Nebraska', '1961455', '1963692', '31'],
['Hawaii', '1451911', '1441553', '15'],
['South Dakota', '887099', '895376', '46'],
['Tennessee', '6920119', '6975218', '47']]

#Solution 1
for sublist in list:
    print(sublist[0]) # Get the first item from the list

#Solution 2
for i in range(len(list)):
    print(list[i][0]) # Get the first item from the list


# Output will like this:
# NAME
# Oklahoma
# Nebraska
# Hawaii
# South Dakota
# Tennessee
-1

You just need to iterate over the data list properly to extract the state names. Currently, your loop is iterating over each element of the data list, but you want to skip the first element (header row) and extract the first element of each subsequent list.

Here's how you can achieve it with a for loop:

import requests

url = f'https://api.census.gov/data/2021/pep/population?get=NAME,POP_2020,POP_2021&for=state:*'
response = requests.get(url)
data = response.json()

def statenames():
    for i in range(1, len(data)):
        state_name = data[i][0]
        print(state_name)

statenames()

In the code above, the loop starts from index 1 (range(1, len(data))) instead of 0, so it skips the header row. For each iteration, it extracts the state name from the first element (data[i][0]) of each list and prints it.

This will produce the following output:

Oklahoma
Nebraska
Hawaii
South Dakota
Tennessee
Elie Hacen
  • 372
  • 12
  • Please do not teach [unidiomatic](https://stackoverflow.com/questions/4383250/) approaches or infer things outside of the question. Skipping the first element can be [done more elegantly](https://stackoverflow.com/questions/10079216), and also wasn't described as part of OP's requirement. Aside from that, questions where the underlying issue is this simple, should always be suspected as duplicates. – Karl Knechtel Jul 30 '23 at 05:38
  • is this chatgpt too? – starball Aug 15 '23 at 08:28