Access nth item from list of lists when list has no particular pattern

Question:

I have this list of lists that has a weird output.

['-', ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], '-', ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], '-', ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], '-', ['fe80::a108:b52e:cfba:a646', '172.29.1.90']]

I need to access the ipv4 items only (2nd position), but at the same time I also need the '-' items.

So the output should be:

['-',172.29.1.90','172.29.1.90','172.29.1.90','172.29.1.90','172.29.1.90','172.29.1.90','-','172.29.1.90','172.29.1.90','172.29.1.90','172.29.1.90','172.29.1.90','172.29.1.90','-','172.29.1.90','172.29.1.90','172.29.1.90','172.29.1.90','-','172.29.1.90']

This would work if it weren’t for the pesky ‘-‘ item.

filtered_host_ip_list = [ip[1] for ip in host_ip_list]

Any ideas? Thanks for chiming in.

Asked By: OverflowStack

||

Answers:

You can filter on wether the item is a list or not

filtered_host_ip_list = [item[1] if isinstance(item,list) else item for item in host_ip_list]
Answered By: azro

Add an "if" to your comprehension

The "if" handles the "-", and the comprehension is otherwise the same as your existing one.

host_ip_list = ['-', ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], '-', ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], '-', ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], '-', ['fe80::a108:b52e:cfba:a646', '172.29.1.90']]

filtered_host_ip_list = [ip[1] if ip != "-" else "-" for ip in host_ip_list ]

print(filtered_host_ip_list)

Result:

['-', '172.29.1.90', '172.29.1.90', '172.29.1.90', '172.29.1.90',
'172.29.1.90', '-', '172.29.1.90', '172.29.1.90', '172.29.1.90', 
'172.29.1.90', '172.29.1.90', '172.29.1.90', '-', '172.29.1.90', 
'172.29.1.90', '172.29.1.90', '172.29.1.90', '-', '172.29.1.90']
Answered By: ProfDFrancis

If the lists can contain more than one IPv4 address, you might want to generalize this to capture them all.

from re import match

ips = ['-', ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], '-', ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], '-', ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], ['fe80::a108:b52e:cfba:a646', '172.29.1.90'], '-', ['fe80::a108:b52e:cfba:a646', '172.29.1.90']]

filtered_ips = [
  ip
  for ip in ips
  for ip in ([ip] if ip == '-' else 
             [i for i in ip if re.match(r"d{1,3}.d({1,3}.d{1,3}.d{1,3}", i)])
]
Answered By: Chris

This would work if it weren’t for the pesky ‘-‘ item.

filtered_host_ip_list = [ip[1] for ip in host_ip_list]

Then add another - 🙂

filtered_host_ip_list = [ip[-1] for ip in host_ip_list]

Index -1 gives you what you want in both cases.

Answered By: Kelly Bundy
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.