1

I need some smaller lists for API compatibility and I was going along the mental track of this poor guy who was raked for approaching this problem in the wrong way.

Python: Assign each element of a List to a separate Variable

except nobody actually told him how to approach the problem in the correct way so I thought I would ask. What is the best way to assign variables over a split list for this type of task? I have a list of query numbers that I am breaking apart in an orderly nonrandom fashion. I have seen the post

Python: Efficient way to split list of strings into smaller chunks by concatenated size

but that seems like quite unnecessary for my predicament.

here is what I am attempting- this gives me 500 query terms at a time and nobody gets overloaded.

link = pd.read_csv(linkfile, dtype = object)
pmids = list(link['PMID'])
split = [pmids[i:i+500] for i in range(0,len(pmids),500)]

My next thought was to assign variables like split0 to split[0] and so forth, so why is that so wrong?

EDITED: I had a typo somewhere- split[0] functioned perfectly in my query

MissBleu
  • 175
  • 2
  • 15
  • This is still not a [MCVE]. You claim `split[0]` doesn't work while `split0`, created with (if I'm reading correctly) `split0 = split[0]` works, but in the code as written, that's impossible. Using `split[0]` would be exactly equivalent, so what you've provided isn't reproducible by anyone. – ShadowRanger Feb 02 '18 at 23:45
  • hmmm I wonder how this could be! it honestly isn't working for me. I will try again as it would certainly save me a lot of work – MissBleu Feb 03 '18 at 00:03
  • 1
    Well, ShadowRanger thanks for calling me out! something else must have been wonky in my code as simply retrying my initial solution (split[0]) solved my problem. (FacePalm) – MissBleu Feb 03 '18 at 00:07

1 Answers1

1

It's wrong because, given a number 1, there isn't a nice way to get the variable list1. But, given a list mylist, it's easy to get the element at index 1:

mylist[1]

So what you want is a list of lists, not a bunch of variables.

wim
  • 338,267
  • 99
  • 616
  • 750
  • So, I have a list of lists, but require variable input to feed into the biopython query. So... it seems this is a catch 22. – MissBleu Feb 02 '18 at 23:14
  • 1
    What do you mean by "require variable input"? Generally Python functions don't care about the original variable names of their arguments (in fact they don't even know, though you can introspect it), they only care about the objects themselves. – wim Feb 02 '18 at 23:19
  • I will edit my question to include the query code – MissBleu Feb 02 '18 at 23:23