Is there method like python popitem for associative arrays in dlang?

I want to get any key/value pair from associative array and remove it. In python it's:

key, value = assoc.popitem()

In D I do:

auto key = assoc.byKey.front; auto value = assoc[key]; assoc.remove(key);

Is there better way to do this? Is it possible to use byKeyValue() outside foreach?

DMD 2.067.1

-------------Problems Reply------------

Is it possible to use byKeyValue() outside foreach?


import std.stdio;

void main()
int[string] assoc = ["apples" : 2, "bananas" : 4];

while (!assoc.byKeyValue.empty)
auto pair = assoc.byKeyValue.front;
writeln(pair.key, ": ", pair.value);

Is there better way to do this?

I don't think D has a library function equivalent for popitem.

Before even thinking about it, I'd point out that you could write a simple function:

import std.typecons;

Tuple!(K, V) popitem(K, V)(ref V[K] arr) {
foreach(k, v; arr) {
return tuple(k, v);
throw new Exception("empty!");
void main() {
int[string] cool;
cool["ten"] = 10;
cool["twenty"] = 20;
import std.stdio;

Or using byKeyValue:

auto popitem(K, V)(ref V[K] arr) {
foreach(item; arr.byKeyValue()) {
return item;
throw new Exception("empty!");
void main() {
int[string] cool;
cool["ten"] = 10;
cool["twenty"] = 20;
import std.stdio;
auto item = cool.popitem();
writeln(item.key, item.value);
item = cool.popitem();
writeln(item.key, item.value);

Generally, I like to encourage people not to be afraid of writing their own functions. If you can express something with a few existing things, just write your own function, give it a name you like, and use that! With the uniform function call syntax, you can easily even write extension methods for built in types, like I did here, and use it as if it has always been there.

Category:d Views:0 Time:2019-02-09

Related post

  • Is there something like python's interactive REPL mode, but for Java? 2008-12-29

    Is there something like python's interactive REPL mode, but for Java? So that I can, for example, type InetAddress.getAllByName( localHostName ) in a window, and immediately get results, without all this public static void nightmare() thing? --------

  • Is there something like Python's getattr() in C#? 2008-09-26

    Is there something like Python's getattr() in C#? I would like to create a window by reading a list which contains the names of controls to put on the window. --------------Solutions------------- There is also Type.InvokeMember. public static class R

  • Is there anything like Winsplit Revolution for Mac OS X? 2008-11-07

    Is there anything like Winsplit Revolution for Mac OS X? --------------Solutions------------- Try these: Zooom/2 ($15) has been my favorite since I installed it. Fast, flexible, and minimizes the number of key combinations I need to remember Divvy ($

  • Is there something like "New Relic" for Perl apps? 2011-11-22

    We have successfully migrated all of our PHP and Ruby apps to use New Relic RPM both for Application performance measurements and server monitoring. We are very please with results, which have enabled us to improve the overall performance of the plat

  • Is there a consistent way to use associative arrays in Cake Session Component methods? 2012-02-29

    I ask because the inability to use associative arrays which contain decimal or integer values means I cannot store the shoe size in the session. e.g. the following does not work: $shoe_id = 'Shoe_1'; $size = '38.5'; $qu = 1; $this->Session->wri

  • Is there something like onclick/onfocus for tabhost/tabspec? 2012-02-09

    I've a fully working tabhost/tabspec app (first working self-made app ever :D) see code beneath. Now, the following: when tab NORM is pressed I want to run a few lines of code. Is there something like an onclick for the tabhost/tabspec. Any help appr

  • Is there something like for business , which allows personalised domain name (like gmail)?? If yes how to subscribe 2014-11-15

    Hi Is there something like for business , which allows personalised domain name (like gmail)?? If yes how to subscribe My Company requires around 500 email ids. Currently we are using Yahoo Business mail, will it be possible to migrate al

  • Is there anything like HTTP::Recorder for Python? 2009-07-10

    I really like Perl's HTTP::Recorder. Is there something like it for Python? --------------Solutions------------- I'm aware of Scotch and FunkLoad, but I don't know how they compare with HTTP::Recorder. See the following links for more details: http:/

  • Is there anything like Python's ctype for PHP? Accessing libraries without the need to write an extension? 2010-01-28

    Python has ctypes to access libraries. In PHP you write extensions for everything. Popular extensions like the one for libgd are available almost everywhere. Is there any extension which works like Python's ctypes, letting you access libraries withou

  • Is there anything like Python's urllib for Common Lisp? 2011-06-23

    I am looking for something like Python's urllib that I can use in Common Lisp. All I am really interested in is the ability to fetch a URL and have a file stream that can be read to produce the requested page's HMTL source code. Exactly as urllib.url

  • Is there anything Like as WMI for Windows same for Linux 2011-03-11

    We would like to develop an test tool to work on Linux os systems Is there anything like WMI for linux to establish remotely connectons and execute commands ,get system infos. Regards, M.Channabasappa --------------Solutions------------- The standard

  • Is there something like .NET Reflector for Qt? 2011-01-10

    Once I've seen a nice tool called .NET Reflector. It can show the entire object hierarchy of .Net binaries/apps (sorry if the term is wrong). Is there something like this for Qt? As Qt has very good QMetaObject abilities, it should be possible to tra

  • Is there a pure Python library for parsing a Windows Registry file? 2011-04-29

    Is there a pure Python (ie. fully cross-platform) library for parsing Windows Registry files (NTUSER.DAT)? Read-only access is acceptable. If there is not, what resources exist that document the reverse-engineered structure of the Registry files? Tha

  • Is there anything like Grails taglibs for Ruby on Rails? 2011-05-01

    I have a few months of experience with Ruby on Rails and really loved it from the beginning on. Now, for work, I was asked to code in Grails and although I first had some objections, I now think it's a very decent framework. One thing that really imp

  • iteratively associate values to variables in C# like python does for (red, green, blue) in [2, 1, 3] 2012-04-26

    think about the array [1, 2, 3] as one instance of his permutations. In my code I need to associate this values to my variables so that I can refer them by name I'm trying to obtain readable and simple code as Python version In Python, if you have: p

  • Is there a good Python package for the Amazon API? 2008-09-07

    What Python libraries do folks use for querying Amazon product data? (Amazon Associates Web Service - used to be called E-Commerce API, or something along those lines). Based on my research, PyAWS seems okay, but still pretty raw (and hasn't been upd

  • Is there anything like for scala libraries 2010-08-08

    I notice there is an sbaz tool that seems to have similar functionality to the ruby gem tool but I don't see any community site like / Is there something like this around. There are 1084 repositories on github with scala i

  • Is there anything like Python Itertools in Perl? 2011-11-23

    Python has great module for working with iterators called itertools Is there any analog in Perl? I know about Object-Iterate but it has only imap and igrep. --------------Solutions------------- List::Gen does a lot of that. While I don't know exactly

  • array_splice() for associative arrays 2009-11-23

    Say I have an associative array: array( "color" => "red", "taste" => "sweet", "season" => "summer" ); and I want to introduce a new element into it: "texture" => "bumpy" behind the 2nd item but preserving all the array keys: array( "color

  • Nested foreach loops for associative array combinations 2010-06-09

    I have an associative array as follows: $myarray = array('a'=>array(), 'b'=>array(), 'c'=>array(), 'd'=>array()); I want to be able to get all pairs of elements in the array. If it wasn't an associative array, I would use nested for loops

  • codeigniter mulitple LIKE db query using associative array- but all from the same column name-? 2011-01-17

    I'm trying to query my database using codeigniter's active record class. I have a number of blog posts stored in a table. The query is for a search function, which will pull out all the posts that have certain categories assigned to them. So the 'cat

  • shorthand for associative array of Image objects 2012-03-15

    I wanted to make an associative array in Javascript in which I would have a bunch of string keys pointing to Image objects. The reason for this, is I wanted to go about pre-loading and dynamically controlling a lot of images without polluting the glo

  • Iterating through list of keys for associative array in JSON 2009-02-17

    I have an associative array in JSON: var dictionary = { "cats": [1,2,3,4,5], "dogs": [6,7,8,9,10] }; How do I get this dictionary's keys? i.e. I want var keys = ["cats", "dogs"]; --------------Solutions------------- Try this: var keys = []; for (var

  • concat (in JavaScript) not working for associative arrays 2009-08-20

    I have a problem concatenating two associative arrays in JavaScript. Below is the sample code: var firstArray = new Array(); firstArray.c1 = "sam"; firstArray.c2 = "kam"; var secArray = new Array(); secArray.c3 = "sam"; secArray.c4 = "kam"; var res =

Copyright (C), All Rights Reserved.

processed in 0.227 (s). 11 q(s)