Remove key:value pair from dictionary based on a condition?Remove key-value pair based on grouping from dictionaries in pythonHow to remove an element from a list by index?How to remove items from a list while iterating?Delete an element from a dictionaryHow to remove a key from a Python dictionary?Select rows from a DataFrame based on values in a column in pandasPythonic way to delete an item completely from a dictionaryCreating a nested dictionary from a list of tuplesPython cant make dictionary from list of dictionariesRemove digits from string pythonRemove nan's from the nested dictionary in python

How did Einstein know the speed of light was constant?

Park the computer

Was the 45.9°C temperature in France in June 2019 the highest ever recorded in France?

What do I need to see before Spider-Man: Far From Home?

What are some bad ways to subvert tropes?

Why isn't addition defined this way?

What is the shape of the upper boundary of water hitting a screen?

Does 5e have an equivalent of the Psychic Paper from Doctor Who?

What happens if the limit of 4 billion files was exceeded in an ext4 partition?

Is it acceptable that I plot a time-series figure with years increasing from right to left?

Should I warn my boss I might take sick leave?

Stay in US on J-1 visa after quitting job?

Is this standard Japanese employment negotiations, or am I missing something?

What can a novel do that film and TV cannot?

Why did Super-VGA offer the 5:4 1280*1024 resolution?

What's the big deal about the Nazgûl losing their horses?

Sleepy tired vs physically tired

Is there a way to change the aspect ratio of a DNG file?

Does the Milky Way orbit around anything?

Did Stalin kill all Soviet officers involved in the Winter War?

Why is there paternal, for fatherly, fraternal, for brotherly, but no similar word for sons?

Why do most airliners have underwing engines, while business jets have rear-mounted engines?

Why weren't Gemini capsules given names?

Do intermediate subdomains need to exist?



Remove key:value pair from dictionary based on a condition?


Remove key-value pair based on grouping from dictionaries in pythonHow to remove an element from a list by index?How to remove items from a list while iterating?Delete an element from a dictionaryHow to remove a key from a Python dictionary?Select rows from a DataFrame based on values in a column in pandasPythonic way to delete an item completely from a dictionaryCreating a nested dictionary from a list of tuplesPython cant make dictionary from list of dictionariesRemove digits from string pythonRemove nan's from the nested dictionary in python






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have nested dictionary 'my_dict' as given below. I want to remove common keys from nested dictionary grouped by main key name format.



my_dict = 'abc_1': '00000000': 0.01555745891946835,
'facility': 0.04667237675840505,
'among': 0.01555745891946835,
'abc_2': '00000000': 0.01555745891946835,
'before': 0.04667237675840505,
'last': 0.01555745891946835,
'mno_1': 'hello': 0.01555745891946835,
'hola': 0.04667237675840505,
'0000150000': 0.01555745891946835,
'mno_2': 'hello': 0.01555745891946835,
'name': 0.04667237675840505,
'0000150000': 0.01555745891946835,
'oko_1': 'err': 0.01555745891946835,
'error': 0.04667237675840505,
'7812': 0.01555745891946835,
'oko_2': '9872': 0.01555745891946835,
'error': 0.04667237675840505,
'00': 0.01555745891946835


For example, common keys in nested dictionary for keys starting abc* is 00000000. So, I want to remove this key. Likewise, i want to do for all.
Expected result is given below:



Expected Result:



result_dict = 'abc_1': 'facility': 0.04667237675840505,
'among': 0.01555745891946835,
'abc_2': 'before': 0.04667237675840505,
'last': 0.01555745891946835,
'mno_1': 'hola': 0.04667237675840505,
'mno_2': 'name': 0.04667237675840505,
'oko_1': 'err': 0.01555745891946835,
'7812': 0.01555745891946835,
'oko_2': '9872': 0.01555745891946835,
'00': 0.01555745891946835









share|improve this question



















  • 1





    What have you tried so far?

    – Stephen Newell
    Mar 25 at 19:34











  • Is it the common key itself, or only if the common keys share a value?

    – chepner
    Mar 25 at 19:52











  • only if common key share a value. Delete then that particular key:value par in nested dictionary.

    – user15051990
    Mar 25 at 19:53

















0















I have nested dictionary 'my_dict' as given below. I want to remove common keys from nested dictionary grouped by main key name format.



my_dict = 'abc_1': '00000000': 0.01555745891946835,
'facility': 0.04667237675840505,
'among': 0.01555745891946835,
'abc_2': '00000000': 0.01555745891946835,
'before': 0.04667237675840505,
'last': 0.01555745891946835,
'mno_1': 'hello': 0.01555745891946835,
'hola': 0.04667237675840505,
'0000150000': 0.01555745891946835,
'mno_2': 'hello': 0.01555745891946835,
'name': 0.04667237675840505,
'0000150000': 0.01555745891946835,
'oko_1': 'err': 0.01555745891946835,
'error': 0.04667237675840505,
'7812': 0.01555745891946835,
'oko_2': '9872': 0.01555745891946835,
'error': 0.04667237675840505,
'00': 0.01555745891946835


For example, common keys in nested dictionary for keys starting abc* is 00000000. So, I want to remove this key. Likewise, i want to do for all.
Expected result is given below:



Expected Result:



result_dict = 'abc_1': 'facility': 0.04667237675840505,
'among': 0.01555745891946835,
'abc_2': 'before': 0.04667237675840505,
'last': 0.01555745891946835,
'mno_1': 'hola': 0.04667237675840505,
'mno_2': 'name': 0.04667237675840505,
'oko_1': 'err': 0.01555745891946835,
'7812': 0.01555745891946835,
'oko_2': '9872': 0.01555745891946835,
'00': 0.01555745891946835









share|improve this question



















  • 1





    What have you tried so far?

    – Stephen Newell
    Mar 25 at 19:34











  • Is it the common key itself, or only if the common keys share a value?

    – chepner
    Mar 25 at 19:52











  • only if common key share a value. Delete then that particular key:value par in nested dictionary.

    – user15051990
    Mar 25 at 19:53













0












0








0








I have nested dictionary 'my_dict' as given below. I want to remove common keys from nested dictionary grouped by main key name format.



my_dict = 'abc_1': '00000000': 0.01555745891946835,
'facility': 0.04667237675840505,
'among': 0.01555745891946835,
'abc_2': '00000000': 0.01555745891946835,
'before': 0.04667237675840505,
'last': 0.01555745891946835,
'mno_1': 'hello': 0.01555745891946835,
'hola': 0.04667237675840505,
'0000150000': 0.01555745891946835,
'mno_2': 'hello': 0.01555745891946835,
'name': 0.04667237675840505,
'0000150000': 0.01555745891946835,
'oko_1': 'err': 0.01555745891946835,
'error': 0.04667237675840505,
'7812': 0.01555745891946835,
'oko_2': '9872': 0.01555745891946835,
'error': 0.04667237675840505,
'00': 0.01555745891946835


For example, common keys in nested dictionary for keys starting abc* is 00000000. So, I want to remove this key. Likewise, i want to do for all.
Expected result is given below:



Expected Result:



result_dict = 'abc_1': 'facility': 0.04667237675840505,
'among': 0.01555745891946835,
'abc_2': 'before': 0.04667237675840505,
'last': 0.01555745891946835,
'mno_1': 'hola': 0.04667237675840505,
'mno_2': 'name': 0.04667237675840505,
'oko_1': 'err': 0.01555745891946835,
'7812': 0.01555745891946835,
'oko_2': '9872': 0.01555745891946835,
'00': 0.01555745891946835









share|improve this question
















I have nested dictionary 'my_dict' as given below. I want to remove common keys from nested dictionary grouped by main key name format.



my_dict = 'abc_1': '00000000': 0.01555745891946835,
'facility': 0.04667237675840505,
'among': 0.01555745891946835,
'abc_2': '00000000': 0.01555745891946835,
'before': 0.04667237675840505,
'last': 0.01555745891946835,
'mno_1': 'hello': 0.01555745891946835,
'hola': 0.04667237675840505,
'0000150000': 0.01555745891946835,
'mno_2': 'hello': 0.01555745891946835,
'name': 0.04667237675840505,
'0000150000': 0.01555745891946835,
'oko_1': 'err': 0.01555745891946835,
'error': 0.04667237675840505,
'7812': 0.01555745891946835,
'oko_2': '9872': 0.01555745891946835,
'error': 0.04667237675840505,
'00': 0.01555745891946835


For example, common keys in nested dictionary for keys starting abc* is 00000000. So, I want to remove this key. Likewise, i want to do for all.
Expected result is given below:



Expected Result:



result_dict = 'abc_1': 'facility': 0.04667237675840505,
'among': 0.01555745891946835,
'abc_2': 'before': 0.04667237675840505,
'last': 0.01555745891946835,
'mno_1': 'hola': 0.04667237675840505,
'mno_2': 'name': 0.04667237675840505,
'oko_1': 'err': 0.01555745891946835,
'7812': 0.01555745891946835,
'oko_2': '9872': 0.01555745891946835,
'00': 0.01555745891946835






python python-3.x dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 20:32









martineau

73.4k10 gold badges101 silver badges191 bronze badges




73.4k10 gold badges101 silver badges191 bronze badges










asked Mar 25 at 19:31









user15051990user15051990

7567 silver badges19 bronze badges




7567 silver badges19 bronze badges







  • 1





    What have you tried so far?

    – Stephen Newell
    Mar 25 at 19:34











  • Is it the common key itself, or only if the common keys share a value?

    – chepner
    Mar 25 at 19:52











  • only if common key share a value. Delete then that particular key:value par in nested dictionary.

    – user15051990
    Mar 25 at 19:53












  • 1





    What have you tried so far?

    – Stephen Newell
    Mar 25 at 19:34











  • Is it the common key itself, or only if the common keys share a value?

    – chepner
    Mar 25 at 19:52











  • only if common key share a value. Delete then that particular key:value par in nested dictionary.

    – user15051990
    Mar 25 at 19:53







1




1





What have you tried so far?

– Stephen Newell
Mar 25 at 19:34





What have you tried so far?

– Stephen Newell
Mar 25 at 19:34













Is it the common key itself, or only if the common keys share a value?

– chepner
Mar 25 at 19:52





Is it the common key itself, or only if the common keys share a value?

– chepner
Mar 25 at 19:52













only if common key share a value. Delete then that particular key:value par in nested dictionary.

– user15051990
Mar 25 at 19:53





only if common key share a value. Delete then that particular key:value par in nested dictionary.

– user15051990
Mar 25 at 19:53












1 Answer
1






active

oldest

votes


















2














First, get all the keys, then filter which keys you wish to keep. Then you can reconstruct the new dict with only the keys to keep:



all_keys = [n for k in my_dict.values() for n in k.keys()]
keys_to_keep = k for k in all_keys if all_keys.count(k) == 1
result_dict = k: kk: v[kk] for kk in keys_to_keep if kk in v for k, v in my_dict.items()


result:



'abc_1': 'facility': 0.04667237675840505, 'among': 0.01555745891946835, 'abc_2': 'before': 0.04667237675840505, 'last': 0.01555745891946835, 'mno_1': 'hola': 0.04667237675840505, 'mno_2': 'name': 0.04667237675840505, 'oko_1': 'err': 0.01555745891946835, '7812': 0.01555745891946835, 'oko_2': '9872': 0.01555745891946835, '00': 0.01555745891946835





share|improve this answer

























  • I have similar kind of problem - stackoverflow.com/questions/55895451/…. Can you please help me in solving it?

    – user15051990
    May 1 at 18:46










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55345171%2fremove-keyvalue-pair-from-dictionary-based-on-a-condition%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














First, get all the keys, then filter which keys you wish to keep. Then you can reconstruct the new dict with only the keys to keep:



all_keys = [n for k in my_dict.values() for n in k.keys()]
keys_to_keep = k for k in all_keys if all_keys.count(k) == 1
result_dict = k: kk: v[kk] for kk in keys_to_keep if kk in v for k, v in my_dict.items()


result:



'abc_1': 'facility': 0.04667237675840505, 'among': 0.01555745891946835, 'abc_2': 'before': 0.04667237675840505, 'last': 0.01555745891946835, 'mno_1': 'hola': 0.04667237675840505, 'mno_2': 'name': 0.04667237675840505, 'oko_1': 'err': 0.01555745891946835, '7812': 0.01555745891946835, 'oko_2': '9872': 0.01555745891946835, '00': 0.01555745891946835





share|improve this answer

























  • I have similar kind of problem - stackoverflow.com/questions/55895451/…. Can you please help me in solving it?

    – user15051990
    May 1 at 18:46















2














First, get all the keys, then filter which keys you wish to keep. Then you can reconstruct the new dict with only the keys to keep:



all_keys = [n for k in my_dict.values() for n in k.keys()]
keys_to_keep = k for k in all_keys if all_keys.count(k) == 1
result_dict = k: kk: v[kk] for kk in keys_to_keep if kk in v for k, v in my_dict.items()


result:



'abc_1': 'facility': 0.04667237675840505, 'among': 0.01555745891946835, 'abc_2': 'before': 0.04667237675840505, 'last': 0.01555745891946835, 'mno_1': 'hola': 0.04667237675840505, 'mno_2': 'name': 0.04667237675840505, 'oko_1': 'err': 0.01555745891946835, '7812': 0.01555745891946835, 'oko_2': '9872': 0.01555745891946835, '00': 0.01555745891946835





share|improve this answer

























  • I have similar kind of problem - stackoverflow.com/questions/55895451/…. Can you please help me in solving it?

    – user15051990
    May 1 at 18:46













2












2








2







First, get all the keys, then filter which keys you wish to keep. Then you can reconstruct the new dict with only the keys to keep:



all_keys = [n for k in my_dict.values() for n in k.keys()]
keys_to_keep = k for k in all_keys if all_keys.count(k) == 1
result_dict = k: kk: v[kk] for kk in keys_to_keep if kk in v for k, v in my_dict.items()


result:



'abc_1': 'facility': 0.04667237675840505, 'among': 0.01555745891946835, 'abc_2': 'before': 0.04667237675840505, 'last': 0.01555745891946835, 'mno_1': 'hola': 0.04667237675840505, 'mno_2': 'name': 0.04667237675840505, 'oko_1': 'err': 0.01555745891946835, '7812': 0.01555745891946835, 'oko_2': '9872': 0.01555745891946835, '00': 0.01555745891946835





share|improve this answer















First, get all the keys, then filter which keys you wish to keep. Then you can reconstruct the new dict with only the keys to keep:



all_keys = [n for k in my_dict.values() for n in k.keys()]
keys_to_keep = k for k in all_keys if all_keys.count(k) == 1
result_dict = k: kk: v[kk] for kk in keys_to_keep if kk in v for k, v in my_dict.items()


result:



'abc_1': 'facility': 0.04667237675840505, 'among': 0.01555745891946835, 'abc_2': 'before': 0.04667237675840505, 'last': 0.01555745891946835, 'mno_1': 'hola': 0.04667237675840505, 'mno_2': 'name': 0.04667237675840505, 'oko_1': 'err': 0.01555745891946835, '7812': 0.01555745891946835, 'oko_2': '9872': 0.01555745891946835, '00': 0.01555745891946835






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 25 at 20:13

























answered Mar 25 at 20:00









Maxime ChéramyMaxime Chéramy

10.5k4 gold badges32 silver badges61 bronze badges




10.5k4 gold badges32 silver badges61 bronze badges












  • I have similar kind of problem - stackoverflow.com/questions/55895451/…. Can you please help me in solving it?

    – user15051990
    May 1 at 18:46

















  • I have similar kind of problem - stackoverflow.com/questions/55895451/…. Can you please help me in solving it?

    – user15051990
    May 1 at 18:46
















I have similar kind of problem - stackoverflow.com/questions/55895451/…. Can you please help me in solving it?

– user15051990
May 1 at 18:46





I have similar kind of problem - stackoverflow.com/questions/55895451/…. Can you please help me in solving it?

– user15051990
May 1 at 18:46








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55345171%2fremove-keyvalue-pair-from-dictionary-based-on-a-condition%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현