1

I've been working on a webpage scraper and I would like to create separate lists containing different elements. There would have to be more than a 1000 lists and I am trying to run that through a for loop. I need the lists to be appropriately named according to the element in each particular iteration. I tried using globals() to achieve this but it only takes an int or a char and not a string. Is there a way to achieve this?

For an example: If people = ['John', 'James', 'Jane'] I need 3 lists named Johnlist=[] Jameslist=[] Janelist=[]

Below is what I tried but it returns an error asking for either an int or a char.

for p in people:
   names = #scrapedcontent
   globals()['%list' % p] = []
   for n in names:
      globals()['%list' % p].append(#scrapedcontent)
1
  • 3
    You don't need this. You want a dictionary. Having random names floating in your namespace is a design mistake that will make your life very difficult Commented Dec 29, 2021 at 10:34

2 Answers 2

2

I strongly discourages you to use globals, locals or vars As suggested by @roganjosh, prefer to use dict:

from collections import defaultdict

people = defaultdict(list):
for p in people:
    for n in names:
        people[p].append(n)

Or

people = {}
for p in people:
    names = #scrapedcontent
    people[p] = names

DON'T USE THIS

for p in people:
    names = [] #scrapedcontent
    globals().setdefault(f'list_{p}', []).extend(names)

Output:

>>> list_John
[]

>>> list_James
[]

>>> list_Jane
[]
Sign up to request clarification or add additional context in comments.

4 Comments

Great suggestion but the data wont work in this way. Each p in the people list has multiple names. its like a relationship list for example JohnList will contain James, Kim, Harry because John has a relation with these names. and I dont believe dictionaries are they way for this. Any other suggestions would really help a lot.
@Glad. globals() returns a dict so a dict is the right way to do what you want and safe.
@Glad. I updated my answer, can you check it please?
That worked! Thank you so much. I realize that its a bad practice but its just a personal project that I'm working on and I need a way to scrape all the data before I can start working on it.
0

try to do it in a dict:

for example:

d = {}

for p in people:
  d[p] = []
  names = ...      
  d[p].extend(names)

1 Comment

Great suggestion but the data wont work in this way. Each p in the people list has multiple names. its like a relationship list for example JohnList will contain James, Kim, Harry because John has a relation with these names. and I dont believe dictionaries are they way for this. Any other suggestions would really help a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.