Home > Net >  Is it more efficient to use set with pairs?
Is it more efficient to use set with pairs?

Time:06-06

I have a dictionary which looks as below:

maps = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}

I have a set which looks as below:

pairs = {("0", "0"), ("1", "1"), ("6", "9"), ("8", "8"), ("9", "6")}

I understand that dictionary gives the ability to define key-value pairs but other than that is it more efficient to use set with pairs as compared to creating a dictionary object?

In what situation, should we use a set with pairs?

CodePudding user response:

There's no point in asking about efficiency, because a map and a set do entirely different things. The immediate question is, what are you trying to do?

Do you need to see what digit goes with another digit? I.e., do you have "6", and want to look it up and get "9"? If so, use a map.

Do you need to see if a particular pair exists? I.e., do you have ("6":"9") and want see if it exists? Then use set.

In regards to efficiency, both are going to be efficient went asked to store what they are made for, and inefficient went coerced into some unintended use.

  • Related