-1

I'm looking for a way to maintain and update a list of IP addresses that I can index into (via a pair of IP addresses) to add a new address to the list.

For example, suppose I have an arbitrary index of (10.0.0.1 & 11.0.0.1). For that pair of indexes, I want a list of other IP addresses. I need to be able to add to the list for the pair 10.0.0.1 & 11.0.0.1 and also use something like the "in" operator to see if the addr I want to add is already present.

Something like this (the indexes are on the left and each index is a list)

(10.0.0.1, 11.0.0.1) --> [1.1.1.1, 6.7.8.9, 120.2.10.7, ...]

(17.7.1.3, 119.4.3.1) --> [33.5.1.81, 14.76.1.28, 14.9.65.19, ...] . . .

I don't really want to bother creating a database at the moment, I just want a quick temporary solution. Some time later, I'll store the info into a database.

Wondering if there are any ideas of collections that I can try.

(See the solution that I posted below, thx to the answers.)

wwwalker
  • 31
  • 8

2 Answers2

3

Dictionaries are adequate for this, as long as the key, i.e. the pair of addresses you want to index by is hashable (e.g. a tuple containing strings).

EDIT: If you haven't used dicts much yet, be aware that its elements are unordered before Python 3.6, and orderedness is only a documented feature starting with 3.7. If you only need to be compatible with Python 3.6 upwards, you may rely on the elements being in ascending order of insertion. Otherwise, you may use collections.OrderedDict instead, from the standard library.

Larry
  • 427
  • 2
  • 10
  • Indeed, a tuple is a perfect device for this. Addresses can be strings or also tuples or even ints if e.g. applying netmasks is desired. – 9000 Oct 05 '22 at 21:03
  • I thought dictionaries could only have a single value for a given key? I want a list of addresses as the value... Maybe I could add the addresses one by one with a comma and then separate via split when I want to operate on the list? – wwwalker Oct 05 '22 at 21:21
  • Oh, I think I see that if I add the addresses as a list, it will work. I'll experiment some more and paste in some lines after I confirm it works for me. – wwwalker Oct 05 '22 at 21:32
  • 1
    @wwwalker The single value for the key can BE a list, it can be anything. – Libra Oct 05 '22 at 22:22
  • @libra Well, it technically can not be a list, as in a list object, because lists are not hashable. However, it can be a tuple, which is also a collection, and a sequence, just like a list. – Larry Oct 05 '22 at 22:57
  • @wwwalker A tuple is not a list. You can not use a list, only a tuple, because the latter is hashable, while the former is not. Both a tuple and a list is a single value, not multiple values. They do provide access to / contain / hold multiple values (elements). Tuples, lists and dictionaries are all collections, meaning they can contain multiple values. Additionally, tuples and lists (but not dictionaries pre-Python 3.6!) are sequences, meaning they are ordered collections, you can iterate through them and find their contained elements in a well defined order. – Larry Oct 05 '22 at 23:00
  • @Larry I suspect we're cross-wired in the discussion... In the solution below, I've used a tuple for each key and the value for each key is a list. – wwwalker Oct 05 '22 at 23:05
  • 1
    @wwwalker I have seen your solution and noticed that you used a tuple, but from your comment, I was not sure whether you understood what a list exactly is, so I found it appropriate to explain the contrast to a tuple. – Larry Oct 05 '22 at 23:07
0

This appears to be working as I was hoping. I appreciate the help and gladly appreciate any additional comments.

Here's what I just tried out (using the arbitrary addresses from my original question):

>>> my_addr_dict = {}
>>> print(my_addr_dict)
{}
>>> print(type(my_addr_dict))
<class 'dict'>
>>>
>>> my_addr_dict[('10.0.0.1','11.0.0.1')]=['1.1.1.1']
>>> my_addr_dict[('10.0.0.1','11.0.0.1')].append('6.7.8.9')
>>> my_addr_dict[('10.0.0.1','11.0.0.1')].append('120.2.10.7')
>>>
>>> print(my_addr_dict)
{('10.0.0.1', '11.0.0.1'): ['1.1.1.1', '6.7.8.9', '120.2.10.7']}
>>>
>>> print(type(my_addr_dict['10.0.0.1','11.0.0.1']))
<class 'list'>
>>>
>>> my_addr_dict[('17.7.1.3','119.4.3.1')]=['33.5.1.81']
>>> my_addr_dict[('17.7.1.3','119.4.3.1')].append('14.76.1.28')
>>> my_addr_dict[('17.7.1.3','119.4.3.1')].append('14.9.65.19')
>>>
>>> print(my_addr_dict)
{('10.0.0.1', '11.0.0.1'): ['1.1.1.1', '6.7.8.9', '120.2.10.7'], ('17.7.1.3', '119.4.3.1'): ['33.5.1.81', '14.76.1.28', '14.9.65.19']}
>>>
>>> print(my_addr_dict[('10.0.0.1','11.0.0.1')])
['1.1.1.1', '6.7.8.9', '120.2.10.7']
>>> print(my_addr_dict[('17.7.1.3','119.4.3.1')])
['33.5.1.81', '14.76.1.28', '14.9.65.19']
>>>
wwwalker
  • 31
  • 8