Python Forum
Best way to accommodate unknown number of variables?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best way to accommodate unknown number of variables?
#4
Usually you wouldn't bother to pre-initialize, especially if you didn't know the exact number beforehand. Instead, just process as necessary and append or add to a collection.

results = []
with open("mydata") as f:
    for line in f:
        # process each line as necessary.
        hostname = line.split()[0]
        results.append[hostname]

# all the processed hostnames are now in the results list
...
Mark17 likes this post
Reply


Messages In This Thread
RE: Best way to accommodate unknown number of variables? - by bowlofred - May-20-2022, 05:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create X Number of Variables and Assign Data RockBlok 8 1,027 Nov-14-2023, 08:46 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020