-1

Somewhere I saw a piece of code where you can unpack a list in a for loop. Let's say that I have a list:

row = ['001', '15\n', '963789', '40\n', '741239', '80\n', '985697', '80\n', '854698', '35\n', '965874', '10\n']

what would be the for loop to unpack the list. I saw something similar to:

for emp_id,pay_rate,job1,hours_worked1,job2,hours_worked2,job3,hours_worked3,job4,hours_worked4,job5,hours_worked5 in row:

what's the correct syntax to unpack this list?

Robert
  • 10,126
  • 19
  • 78
  • 130

2 Answers2

3

That only really works if you have a nested structure. For example:

l = [(1,2,3), (4,5,6), (7,8,9)]

for a,b,c in l:
    print a,b,c

[OUTPUT]
1 2 3
4 5 6
7 8 9

If you have a flat list like this:

l2 = [1,2,3,4,5]

You can do:

one, two, three, four, five = l2
sshashank124
  • 31,495
  • 9
  • 67
  • 76
0

Simple assignment:

emp_id,pay_rate,job1 = ['001', '15\n', '963789']
Elazar
  • 20,415
  • 4
  • 46
  • 67