# Partitioning Items into Sub-lists based on list lengths. (Custom Python Code)

I am new to Programming and started learning Python recently. I really appreciate your help regarding this issue!

I have a list of two sub-lists containing two items in each. I also have another list called Parts that dictates the segregation of those items. While the below codes seem to work properly, but don’t do the job right when the sublists of the Parts(partition sizes) list are the same/identical. Need some expert help or advice.

Note: The sum of int in the Parts sublist is equal to the length of the corresponding Items sublist.

``````# The inputs to this node will be stored as a list in the IN variables.
Parts = IN[1]
Items = IN[0]

# Place your code below this line
List = []
for i in Parts:
Temp = []
A = 0
for j in i:
if j == 0:
x = []
else:
x = Items[Parts.index(i)][int(A):int(A+j)]
A = j
Temp.append(x)
List.append(Temp)
# Assign your output to the OUT variable.
OUT = List
``````

Please Do refer to the Screenshot attached for clarity.

Thank you!

``````# The inputs to this node will be stored as a list in the IN variables.
Parts = IN[1]
Items = IN[0]

# Place your code below this line
List = []
B = 0

for i in Parts:
Temp = []
A = 0
for j in i:
if j == 0:
x = []
else:
x = Items[B][int(A):int(A+j)]
A = j
Temp.append(x)
List.append(Temp)
B += 1
# Assign your output to the OUT variable.
OUT = List``````
1 Like

@KiranGolla.BLOX If you don’t want to introduce any new variables then try this:

``````# The inputs to this node will be stored as a list in the IN variables.
Parts = IN[1]
Items = IN[0]

# Place your code below this line
List = []

for i in range(len(Parts)):
Temp = []
A = 0
for j in Parts[i]:
if j == 0:
x = []
else:
x = Items[i][int(A):int(A+j)]
A = j
Temp.append(x)
List.append(Temp)

# Assign your output to the OUT variable.
OUT = List``````
1 Like

@AmolShah
This is great! Thank you so much.
Didn’t think of this logic.
Have you got a StackOverflow Profile/ID? I want to clear it there as well and credit you for the solution!

1 Like

You’re welcome @KiranGolla.BLOX
You can take the credit for yourself, I’m not very active on StackOverflow.

1 Like