Reputation: 31
#creating a list named employees
print(('#'*50).center(100,'~'))
employees=[['Name','Age','Department'],['John Mckee',28,'Sales'],['Lisa Crawford',29,'Marketing'],['Sujan patel',33,'HR']]
#print the top row of the matrix (the column names)
print(employees[0],'\n')
print('LISA DETAILS'.center(100,'-'))
#print only lisa details
print('Name:',employees[2][0])
print('Age:',employees[2][1])
print('Department:',employees[2][2])
#print all employees details
print('ALL DETAILS'.center(100,'-'))
print(employees)
#want to start for loop at Name:John,Age:28,Department:sales, instead of Name:Name,Age:Age,Department:Department
for e in (employees):
print('Name:',e[0])
print('Age:',e[1])
print('Department:',e[2])
Any way that I can start printing from second row (the employees basically) instead of first row (Name, Age, Department)?
I do, not know how I would go on about approaching for loop within a list at different position.
Upvotes: 0
Views: 504
Reputation: 2925
If you need more pythonic approach to what @pakpe presented you can use list slicing
for e in employees[from_index:to_index]:
...
where from_index
and to_index
indicate boundaries and one of them may be omitted if not needed. This creates a copy of the array however. If you don't want to make copies then use itertools.islice()
for e in islice(employees, from_index, to_index):
...
unused parameter should be explicitly set to None
in this case.
Upvotes: 2
Reputation: 5479
Instead of using a for-each loop, use a regular for loop that advances the index from 1 to len(employees):
for i in range(1, len(employees)):
print('Name:',employees[i][0])
print('Age:',employees[i][1])
print('Department:',employees[i][2])
Upvotes: 1