PsiElement ( @NotNull final PyElement list, @Nullable final PyExpression afterThis, @NotNull final PyExpression toInsert) { // TODO: #insertItemIntoList is probably buggy. In such case, fix it and get rid of this method final PsiElement result = insertItemIntoList(list, afterThis, toInsert); final LeafPsiElement[] leafs = PsiTreeUtil.getChildrenOfType(list, LeafPsiElement.class); if (leafs != null) { final Deque<LeafPsiElement> commas = Queues.newArrayDeque(Collections2.filter(Arrays.asList(leafs), COMMAS_ONLY)); if (!commas.isEmpty()) { final LeafPsiElement lastComma = commas.getLast(); if (PsiTreeUtil.getNextSiblingOfType(lastComma, PyExpression.class) == null) { //Comma has no expression after it lastComma.delete(); } } } return result; }