Tony Burns
Tony Burns

Reputation: 55

Search through a nested list using an if statement

I'm trying to take elements out of a nested list that have values below 50 and the corresponding values with them to display them. I tried making it, but it's giving me nothing.

Here is the code:

newList = ["payroll", "accounting", "security", "office", "sales"]
deptNums = [10 * index for index in range(1, 16)]
deptInfo = [[]]

for row in range(0, len(newList)) :
    deptInfo.append([newList[row], deptNums[row]])
print(deptInfo)

belowFifty = []
for items in deptInfo:
        if (50 > deptNums[row]):
            belowFifty.append(newList[row],deptNums[row])
print(belowFifty)

Upvotes: 2

Views: 254

Answers (4)

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

There are two issues: 1. You are initializing deptInfo as [[]], hence after the appends you get:

[[],
 ['payroll', 10],
 ['accounting', 20],
 ['security', 30],
 ['office', 40],
 ['sales', 50]]
  1. You are iterating over the items in deptInfo but trying to check deptNum with indices (row) that was the previous iteration variable, which currently has a value 5, and deptNum[5] has a value of 60, so your if does not check anything. Here is your fixed code:
newList = ["payroll", "accounting", "security", "office", "sales"]
deptNums = [10 * index for index in range(1, 16)]
deptInfo = []

for row in range(0, len(newList)) :
    deptInfo.append([newList[row], deptNums[row]])
print(deptInfo)
belowFifty = []
for items in deptInfo:
        if (50 > items[1]):
            belowFifty.append(items)
print(belowFifty)

Now here's a shortened code:

newList = ["payroll", "accounting", "security", "office", "sales"]
deptNums = [10 * index for index in range(1, 16)]
print([[i,j] for i,j in zip(newList, deptNums) if j < 50])

Upvotes: 2

naive
naive

Reputation: 367

You don't need deptInfo.

Just do this:

belowFifty = []
for row in range(len(deptNums)):
    if (50 > deptNums[row]):
        belowFifty.append([newList[row],deptNums[row]])

belowFifty

Output:

[['payroll', 10], ['accounting', 20], ['security', 30], ['office', 40]]

Upvotes: 2

s3dev
s3dev

Reputation: 9701

A short-hand approach using list comprehension rather than conventional loops.

Is this what you're after? This example uses the zip() function and list comprehension.

Basically, the zip function aligns the two lists and iterates them together - so here, we can put your department names and department numbers together and iterate them as a pair.

newList = ["payroll", "accounting", "security", "office", "sales"]
# I edited this to match the length of your department names.
deptNums = [10 * index for index in range(1, len(newList))]

# Use list comprehension to iterate, test and create a new list all in ONE line.
deptInfo = [[num, dpt] for num, dpt in zip(newList, deptNums) if dpt < 50]

Output:

[['payroll', 10], ['accounting', 20], ['security', 30], ['office', 40]]

Upvotes: 0

Prem Ib
Prem Ib

Reputation: 146

You are not iterating variable 'row' in the second for loop. the scope of variable 'row' ends with the first for loop.A more appropriate code:

newList = ["payroll", "accounting", "security", "office", "sales"]
deptNums = [10 * index for index in range(1, 16)]
deptInfo = [[]]

for row in range(0, len(newList)) :
    deptInfo.append([newList[row], deptNums[row]])
print(deptInfo)

belowFifty = []
for item, number in zip(newList, deptNums):
    if 50 > number:
        belowFifty.append([item, number])
print(belowFifty)

Upvotes: 3

Related Questions