Remove elements from deep nested list

Remove elements from deep nested list



I have a list like this. How do I traverse the sublists to remove all the 1.'s but leaving the structure intact? I have attempted with Drop[list, 1] but this removes the first sublist, and the 1.'s are in deeper sublists.


Drop[list, 1]


1., 1812., 1., 4262., 1., 1272., 1., 4048., 1.,
3961., 1., 7739., 1., 3173., 1., 1201., 1., 6856., 1.,
11860., 1., 1324., 1., 3457., 1., 5038., 1., 18731., 1.,
4624., 1., 6527., 1., 4971., 1., 4936., 1., 2371., 1.,
3750., 1., 5660., 1., 2451., 1., 6293., 1., 4577., 1.,
2005., 1., 1428., 1., 6300., 1., 1145., 1., 2797., 1.,
3096., 1., 3249., 1., 2323., 1., 4063., 1., 3751., 1.,
4430., 1., 2228., 1., 2141., 1., 881.87, 1., 6139., 1.,
5055., 1., 1821., 1., 64625., 1., 6462., 1., 3415., 1.,
5792., 1., 2384., 1., 2193., 1., 2934., 1., 5664., 1.,
10404., 1., 3382., 1., 3105., 1., 7323., 1., 9947., 1.,
6282., 1., 7470., 1., 18522., 1., 3243., 1., 2813., 1.,
5574., 1., 2719., 1., 7508., 1., 3832., 1., 5858., 1.,
4174., 1., 10320., 1., 3402., 1., 10592.,
1., 1154., 1., 2259., 1., 650.77, 1., 1210., 1., 1812., 1.,
2630., 1., 1373., 1., 1028., 1., 2351., 1., 2614., 1.,
1494., 1., 1480., 1., 1903., 1., 2058., 1., 2520., 1.,
3663., 1., 2352., 1., 1950., 1., 1125., 1., 1991., 1.,
2718., 1., 1447., 1., 3137., 1., 2014., 1., 1040., 1.,
735.41, 1., 3531., 1., 597.25, 1., 1181., 1., 2203., 1.,
1837., 1., 1217., 1., 1793., 1., 2520., 1., 1972., 1.,
1021., 1., 1317., 1., 428.54, 1., 3340., 1., 2294., 1.,
1125., 1., 2758., 1., 2510., 1., 1500., 1., 2800., 1.,
1284., 1., 1854., 1., 1567., 1., 3042., 1., 3823., 1.,
1852., 1., 1550., 1., 4062., 1., 5808., 1., 2971., 1.,
3837., 1., 6249., 1., 3008., 1., 1604., 1., 2172., 1.,
1424., 1., 2998., 1., 3669., 1., 2870., 1., 2313., 1.,
5777., 1., 1945., 1., 6480.



EDIT: I'm looking for an operation that targets the position of the 1.




3 Answers
3


list[[All, All, 2;;]] (* or *)
Drop[list, None, None, 1]



1812., 4262., 1272., 4048., 3961., 7739., 3173.,
1201., 6856., 11860., 1324., 3457., 5038., 18731.,
4624., 6527., 4971., 4936., 2371., 3750., 5660.,
2451., 6293., 4577., 2005., 1428., 6300., 1145.,
2797., 3096., 3249., 2323., 4063., 3751., 4430.,
2228., 2141., 881.87, 6139., 5055., 1821., 64625.,
6462., 3415., 5792., 2384., 2193., 2934., 5664.,
10404., 3382., 3105., 7323., 9947., 6282., 7470.,
18522., 3243., 2813., 5574., 2719., 7508., 3832.,
5858., 4174., 10320., 3402., 10592., 1154., 2259.,
650.77, 1210., 1812., 2630., 1373., 1028., 2351.,
2614., 1494., 1480., 1903., 2058., 2520., 3663.,
2352., 1950., 1125., 1991., 2718., 1447., 3137.,
2014., 1040., 735.41, 3531., 597.25, 1181., 2203.,
1837., 1217., 1793., 2520., 1972., 1021., 1317.,
428.54, 3340., 2294., 1125., 2758., 2510., 1500.,
2800., 1284., 1854., 1567., 3042., 3823., 1852.,
1550., 4062., 5808., 2971., 3837., 6249., 3008.,
1604., 2172., 1424., 2998., 3669., 2870., 2313.,
5777., 1945., 6480.


Dimensions @ %, Dimensions[list]



2, 68, 1, 2, 68, 2


SeedRandom[1]
lst = RandomReal[100, 100, 1000, 9];

(res0 = lst[[All,All,2;;]])//RepeatedTiming// First



0.0022


(res1 = Drop[lst, None, None, 1];) // RepeatedTiming // First



0.0034


(res2 = Map[Rest, lst, 2];) // RepeatedTiming // First



0.023


(res3 = Replace[lst, (x_, y__ :> y), 2];) // RepeatedTiming // First



0.139


res0 = res1 == res2 == res3



True



If you want to target lists like 1., 1812., and the level at which they occur can vary, I would use Replace with a negative level:


1., 1812.


Replace


Replace[list, _, b__ :> b, -2]



1812., 4262., 1272., 4048., 3961., 7739., 3173., 1201.,
6856., 11860., 1324., 3457., 5038., 18731., 4624., 6527.,
4971., 4936., 2371., 3750., 5660., 2451., 6293., 4577.,
2005., 1428., 6300., 1145., 2797., 3096., 3249., 2323.,
4063., 3751., 4430., 2228., 2141., 881.87, 6139., 5055.,
1821., 64625., 6462., 3415., 5792., 2384., 2193., 2934.,
5664., 10404., 3382., 3105., 7323., 9947., 6282., 7470.,
18522., 3243., 2813., 5574., 2719., 7508., 3832., 5858.,
4174., 10320., 3402., 10592., 1154., 2259., 650.77, 1210.,
1812., 2630., 1373., 1028., 2351., 2614., 1494., 1480.,
1903., 2058., 2520., 3663., 2352., 1950., 1125., 1991.,
2718., 1447., 3137., 2014., 1040., 735.41, 3531., 597.25,
1181., 2203., 1837., 1217., 1793., 2520., 1972., 1021.,
1317., 428.54, 3340., 2294., 1125., 2758., 2510., 1500.,
2800., 1284., 1854., 1567., 3042., 3823., 1852., 1550.,
4062., 5808., 2971., 3837., 6249., 3008., 1604., 2172.,
1424., 2998., 3669., 2870., 2313., 5777., 1945., 6480.



To remove the 1's specifically:



Something like this uses ReplaceAll to replace all the 1.'s with the Nothing element:


1.


lis = (*your data*)
lis /. (1. -> Nothing)



To remove the first element of each sublist at the 2nd level:



In case you case you aren't necessarily using 1.'s, but want to replace just replace the first part of each deeply nested sublist, consider something like Replace with a level spec:


lis = (*your data*)
Replace[lis, (x_, y__ :> y), 2]



Alternatively:


lis = (*your data*)
Map[Rest, lis, 2]





Thanks, but I'm hoping for something that doesn't target the number 1. but rather that position.
– briennakh
Aug 26 at 2:29





@briennakh I'd mention that in the post, but edited.
– user6014
Aug 26 at 2:32





Thank you, this is very helpful.
– briennakh
Aug 26 at 2:44






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)