# Dropping the 3rd variable in a list of lists [duplicate]

I have the following text: loadeddata={{1,2,3},{5,2,1},{1,4,1},{1,5,0},{2,3,5}}

How can I removed the third numbers in each of the sublists?

The real data has 600 of these sublists. I want to use Histogram3D with the first two variables.

• loadeddata[[All, {1, 2}]] or loadeddata[[All, 1 ;; 2]] Sep 26, 2016 at 2:47
• I think people are too quick to vote close on newbies.
– M.R.
Sep 26, 2016 at 3:41
• If you like to use a pattern then loadeddata /. {a_, b_, c_} -> {a, b} Sep 26, 2016 at 5:32

Here's a convenience function for you:

DropColumn[mat_, n_] := Module[
{toDrop, toKeep, numberOfColumns, normalized, fixNegs},
numberOfColumns = Length[mat[[1]]];
fixNegs = Function[If[Less[#, 0], numberOfColumns + # + 1, #]];
toDrop = If[ListQ[n], Map[fixNegs, n], {fixNegs @ n}];
toKeep = Complement[Range @ numberOfColumns, toDrop];
Return[mat[[All, toKeep]]]
];


Here's how you use it:

loadeddata = {{1, 2, 3}, {5, 2, 1}, {1, 4, 1}, {1, 5, 0}, {2, 3, 5}};

• Can you explain what advantage it has over loadeddata[[All, 1 ;; 2]]?