[Tutor] List slicing and joining

Andreas Kostyrka andreas at kostyrka.org
Wed Apr 11 22:11:50 CEST 2007

* Ben Sherman <bensherman at gmail.com> [070411 22:02]:
> I've got a list that contain a bunch of information, including the
> FQDN of a host.
> host_data=['foo.example.com', 'other unimportant data']
> I need to seperate the hostname from the domain name.
> This is how I'm doing it, and it work, but it seems *really* hacky.
> Is there a better (or more pythony) way?
> hostname=host_data[0].split(".")[0]
> domain=".".join(host_data[0].split(".")[1:])

Well, it's basically ok, but I'd probably do something like this:
fqdn_parts = host_data[0].split(".")
host = fqdn_parts[0]
domain = ".".join(fqdn_parts[1:])

Alternativly, if you are sure that you've got a '.' in the host_part,
you can do:

host, domain = host_data[0].split(".", 1)


More information about the Tutor mailing list