Get results from a for loop in Python

106 Views Asked by At

I am new to coding in Python and ran into an issue. I have a list of domain names that I would like to get whois lookup information of. I am using a for look to get whois information on every domain in the list called domain_name like this:

for i in domain_name:
    print(whois.whois(i))

I am getting the results printed just fine. But I would like to save those results in a variable that I can make a list of dataframe out of. How do I go about doing that?

thank you!

2

There are 2 best solutions below

1
Ari Cooper-Davis On

Define the list you want to store them in before the loop then append them to it inside the loop:

my_container = []
for domain in domain_names:
   my_container.append(whois.whois(domain))
1
Zhenhir On

A list comprehension is appropriate here, useful if you are starting with one list and want to create a new one.

my_results = [whois.whois(i) for i in domain_name]

Will create a new list with the whois results.