Reputation: 55
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
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]]
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
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
Reputation: 9701
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]
[['payroll', 10], ['accounting', 20], ['security', 30], ['office', 40]]
Upvotes: 0
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