Python Dictionary:removing a string from a tuple( WHICH is a key .)


I've got a dictionary like

dic ={('L', 'N', 'C'):6, ('N', 'L', 'C'):4, ('C', 'N', 'L'):3})

I want to remove the string 'C' from all keys. Is there any efficient way of doing so


This can be done with a single <a href="http://www.python.org/dev/peps/pep-0274/" rel="nofollow">dictionary comprehension</a>:

>>> dic ={('L', 'N', 'C'):6, ('N', 'L', 'C'):4, ('C', 'N', 'L'):3} >>> {tuple(l for l in k if l != 'C'):v for k,v in dic.items()} {('L', 'N'): 6, ('N', 'L'): 4}

Note that the removal of 'C' makes ('N', 'L', 'C') and ('C', 'N', 'L') clash as they both become ('N', 'L'). It is not clear from the question how you wish to handle that.


for k,v in dic.iteritems(): if 'C' in k: dic[tuple(el for el in k if el!='C')] = dic.pop(k)

In this solution,<br /> only the keys that contain the element to eliminate are changed (new objects since tuples are immutable).<br /> The dictionary is modified in place<br /> and the values remain the same objects.

The following code shows that.

dico = {(1,2,8):'aa', (25,8,45,9):'gerard', (268,54,0):'marine', (81,3,8,7):'emma', (7,9,6):'louis'} print ' id(dico) : ',id(dico) for k,v in dico.iteritems(): print id(k),'%-25s' % repr(k),id(v),v idk = [id(el) for el in dico] for k,v in dico.iteritems(): if 8 in k: dico[tuple(el for el in k if el!=8)] = dico.pop(k) print print ' id(dico) : ',id(dico) for k,v in dico.iteritems(): print '%d %-30s %d %s' %\ (id(k), ('[new id] ' if id(k) not in idk else ' ')+repr(k),id(v),v)


id(dico) : 18737456 18751976 (268, 54, 0) 18718464 marine 11258576 (1, 2, 8) 18566968 aa 18539072 (25, 8, 45, 9) 18603776 gerard 18606768 (81, 3, 8, 7) 18718432 emma 18752056 (7, 9, 6) 18718592 louis id(dico) : 18737456 18752216 [new id] (1, 2) 18566968 aa 18751976 (268, 54, 0) 18718464 marine 18752176 [new id] (81, 3, 7) 18718432 emma 18752056 (7, 9, 6) 18718592 louis 18752416 [new id] (25, 45, 9) 18603776 gerard


  • Tablesorter combining sortList with disabled columns
  • Get a unique list of items that occur more than once in a list
  • testing postgres db python
  • I have a twisted reactor running, how do I connect to it?
  • What object to pass to R from rpy2?
  • Using string-type resource identifiers in Windows .rc files / dlls
  • Multidimensional data and plot legends
  • What's the difference between [String] vs. [(String)]?
  • Implementing multiple instances of the same generic Java interface with different generic types?
  • finding symmetric difference/unique elements in multiple arrays in javascript
  • Matrix problem Python
  • Grouping vars in function
  • What is wrong with this emulation of CMPXCHG16B instruction?
  • Click button with javascript
  • Grouping by blank nodes
  • How do I translate LR(1) Parse into a Abstract syntax tree?
  • Is it possible to define rest argument in OCaml?
  • Python pickle not one-to-one: different pickles give same object
  • Assign variable to the value in HTML
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Debug.DrawLine not showing in the GameView
  • How to use carriage return with multiple line?
  • Django rest serializer Breaks when data exists
  • Alert pop up with LWUIT
  • Ajax Loaded meta Tags
  • How to rebase a series of branches?
  • Xamarin Forms - UWP Fonts
  • CSS Linear-gradient formatting issue accross different browsers
  • Spring Data JPA custom method causing PropertyReferenceException
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Azure Cloud Service Web Role web pages do not load
  • Excel - Autoshape get it's name from cell (value)
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Perl system calls when running as another user using sudo
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • what is the difference between the asp.net mvc application and asp.net web application
  • Arrays break string types in Julia
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Python/Django TangoWithDjango Models and Databases