[Tutor] List slicing and joining
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?
Well, it's basically ok, but I'd probably do something like this:
fqdn_parts = host_data.split(".")
host = fqdn_parts
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.split(".", 1)
More information about the Tutor