Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through Dictionary [duplicate]

Tags:

c#

I have a dictionary with a Person and a Count value:

Dictionary<string, int> PersonDictionary = new Dictionary<string, int>();

It has the following values:

Sally, 6
Beth, 5
Mary, 5

I want to alternate each person and decrement the value by 1 each time it goes through the loop. I'm running into a real mind block on this one

What is the best way to get Sally and decrement by 1 and then go to Beth and decrement by 1 and then go to Mary and decrement by 1 and then go back to Sally... So on and so forth.

Just add further clarification I want to loop through this and use the owner.Key value and pass that to another method. So I need to be able to loop through this dictionary 1 at a time.

Update:

There are a few issues with my question. One issue was the decrementing the dictionary while in a loop. But my main question was how to iterate through each item [Sally -> Beth -> Mary -> Sally) until each persons value goes to 0 - That part is still the question at large.

like image 821
webdad3 Avatar asked Jun 03 '18 16:06

webdad3


People also ask

How do you iterate through a dictionary in a for loop?

This means that if you put a dictionary directly into a for loop, Python will automatically call .__iter__ () on that dictionary, and you’ll get an iterator over its keys: Python is smart enough to know that a_dict is a dictionary and that it implements .__iter__ ().

What happens when you loop through a complex Dictionary?

As it is in a regular dictionary, looping out the entire items outputs all key-value pairs in individual tuples: You can also see specific values in a dictionary containing other dictionaries. But keep in mind that the values of a complex dictionary are the items of other dictionaries inside it.

What is looping in Python dictionary?

This technique allows you to read, manipulate, and output the contents of a dictionary. Looping in Python is easy. But beginners might find it a bit confusing, especially when using it with a more complex iterable such as a dictionary. Here are some of the ways you can deal with a Python dictionary using loops.

How to return values from looping through a dictionary?

When looping through a dictionary, the return value are the keysof the dictionary, but there are methods to return the valuesas well. Example Print all key names in the dictionary, one by one: for x in thisdict: print(x) Try it Yourself »


2 Answers

Using Linq it would be as follows:

personDictionary = personDictionary.ToDictionary(e => e.Key, e => e.Value-1);
like image 70
Ousmane D. Avatar answered Nov 03 '22 10:11

Ousmane D.


The above DOESN'T work.

It doesn't work because it's not possible to modify the collection inside of foreach loop and iterate through it at the same time.

That's why when we loop through the dictionary and try to modify it - an exception is thrown.

In order to change the same values without creating a copy of them we could iterate through a copy of Keys collection and use indexer:

foreach (var ownerKey in PersonDictionary.Keys.ToList())
{
    PersonDictionary[ownerKey]--;
}

This will copy the Keys but the Dictionary object itself and Values collection will remain the same. Or you could use Aominès solution if making a copy of the entire dictionary is not an issue.

like image 41
Fabjan Avatar answered Nov 03 '22 09:11

Fabjan