-1

I have a list of lists containing some data and I would like to be able to call the contents of the list of lists but only certain things ex: empList[0][0], empList[0][1]. I would like to be able to do that to each item in the list and then call the selected index from that part of the list ex: empList[0][0], empList[0][1], empList[1][0], empList[1][1] but in a for loop. My code for reference:

empList = [
    ['51-4678119', 'Issie', 'Scholard', '11 Texas Court', 'Columbia', 'Missouri', '65218', '3', '134386.51', '34', '91.06'],
    ['68-9609244', 'Jed', 'Netti', '85 Coolidge Terrace', 'San Antonio', 'Texas', '78255', '2', '159648.55', '47', '45.7'],
    ['47-2771794', 'Galvan', 'Solesbury', '3 Drewry Junction', 'Springfield', 'Illinois', '62794', '2', '91934.89', '39', '47.92']
]
emp = employee(empList[0][0], empList[0][1], empList[0][2], empList[0][3], empList[0][4], empList[0][5], empList[0][6], empList[0][7])

Hopefully this makes sense. Thank you!

gdm
  • 59
  • 1
  • 6
  • Does this answer your question? [Iterating through list of list in Python](https://stackoverflow.com/questions/6340351/iterating-through-list-of-list-in-python) – Jim G. May 04 '21 at 16:01

4 Answers4

1

If the arguments are always in the right order, you can do:

employees = [employee(*args) for args in empList]
flakes
  • 21,558
  • 8
  • 41
  • 88
1

you can use:

emp = employee(*empList[0][:8])
Nk03
  • 14,699
  • 2
  • 8
  • 22
1

This might resemble what you are looking for:

from collections import namedtuple

empList = [
    ['51-4678119', 'Issie', 'Scholard', '11 Texas Court', 'Columbia', 'Missouri', '65218', '3', '134386.51', '34', '91.06'],
    ['68-9609244', 'Jed', 'Netti', '85 Coolidge Terrace', 'San Antonio', 'Texas', '78255', '2', '159648.55', '47', '45.7'],
    ['47-2771794', 'Galvan', 'Solesbury', '3 Drewry Junction', 'Springfield', 'Illinois', '62794', '2', '91934.89', '39', '47.92']
]
Employee = namedtuple('Employee', 'ID First Last Street City State Zip Ham Salary Eggs Spam')
employees = [Employee(*e) for e in empList]

print(employees[0])
print(employees[1].State)
print(employees[2].Ham)

Employee(ID='51-4678119', First='Issie', Last='Scholard', Street='11 Texas Court', City='Columbia', State='Missouri', Zip='65218', Ham='3', Salary='134386.51', Eggs='34', Spam='91.06')
Texas
2
Jamie Deith
  • 706
  • 2
  • 4
-1
for i in range (len(empList)):
emp = employee(empList[i][0], empList[i][1], empList[i][2], empList[i][3], empList[i][4], empList[i][5], empList[i][6], empList[i][7])
empList1.append(emp)

this for loop aloud me to loop through each item in the list of lists and to only get the enrties I needed.

gdm
  • 59
  • 1
  • 6