-1

I have a nested list of items:

urls1 = ["https://stackoverflow.com/questions/61441230/typeerror-list-indices-must-be-integers-or-slices-not-list",
         "https://docs.python.org/2.3/whatsnew/section-enumerate.html",
         "https://docs.python.org/3/tutorial/introduction.html#lists"]
urls2 = ["https://stackoverflow.com/questions/6340351/iterating-through-list-of-list-in-python",
         "https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists"]
urllist = [urls1, urls2]

and a nested loop:

container = []
for i in urllist:
    for u in i:
        filename = u.split('/')[-1] #etc.. this loop is not important
name = str(i)
print(name)

This prints the elements (the URLs) from the lower-level lists, when I am trying to print the names OF the two items in urllist (top level) as strings.

I am trying to get something like str(urllist)[i] or str(urllist[i]).

I also tried calling the names with the numeric index like so:

container = []
for idx, i in enumerate(urllist):
    for u in i:
        filename = u.split('/')[-1] #etc.. this loop is not important
name = i[idx]
container.append(name)
print(name)

This allows me to subset by idx, which is numeric, but it still prints the URLs and not the names of the objects in my top level list.

Is there a way to call my list items by name as strings? I have looked at similar questions but not found an answer that works (Python documentation only shows this for items that are already strings).

Edit:

I would like it to print urls and urls2 (the two items in my top level list) as strings.

martineau
  • 119,623
  • 25
  • 170
  • 301
cmoez
  • 1
  • 1
  • 2
    What do you mean "the names". Objects don't have names. What do you want `print(name)` to print? – juanpa.arrivillaga Nov 29 '20 at 00:30
  • 1
    Show your desired outcome. Don't describe it. Post it as text. –  Nov 29 '20 at 00:33
  • Thanks, I edited it. I am trying to get "urls1", "urls2" to print as strings. I am used to R where I can assign names to list objects. – cmoez Nov 29 '20 at 00:40
  • 2
    Python doesn't work like that. Python lists don't support element names, and most objects have no concept of a name. Variables have names, but objects have no information about what variables refer to them. – user2357112 Nov 29 '20 at 00:41
  • 1
    If you search hard enough, you'll find janky, slow, unreliable ways to search for variables that hold references to an object, but there are all sorts of aliasing issues, and the performance is atrocious. You'll be much better served by using a dict or something. – user2357112 Nov 29 '20 at 00:43
  • I need the objects in my list as strings, because I will need to paste these strings into filenames later and there are many top level elements in my actual list. – cmoez Nov 29 '20 at 00:49
  • In that case create a dictionary that maps each of the names (`"urls1"` and `"urls2"`) to the corresponding objects (`urllist[0]` and `urllist[1]`). – martineau Nov 29 '20 at 01:44
  • Thanks, will try the dictionary approach – cmoez Nov 29 '20 at 02:15
  • @cmoez then *you should create a data structure that bundles the string and the other data you need*. This could be as simple a list of tuples, but you could also just create a custom class to do this. – juanpa.arrivillaga Nov 29 '20 at 02:33

1 Answers1

-1
for urls in urllist:
    for url in urls:
        filename = url .split('/')[-1]
        print( filename )

typeerror-list-indices-must-be-integers-or-slices-not-list
section-enumerate.html
introduction.html#lists
iterating-through-list-of-list-in-python
flatten-an-irregular-list-of-lists


Edit:
These 3 methods all return the same thing. I think that's what you're talking about.

print( urls1, '\n' )
print( urls2, '\n' )

print( '-----------------------------------------' )


print( urllist [0], '\n' )
print( urllist [1], '\n' )

print( '-----------------------------------------' )

for url in urllist:
    print( url, '\n' )
Doyousketch2
  • 2,060
  • 1
  • 11
  • 11
  • You're looping through it to get the contents, right? – Doyousketch2 Nov 29 '20 at 00:41
  • Are these just existing pages? I am trying to keep the list nested, if anything the results are too flattened right now. The only pages I saw on calling top level items were for crawling directories with os.walk – cmoez Nov 29 '20 at 00:41
  • No, I don't want the contents, I want it to print "urls1", "urls2" – cmoez Nov 29 '20 at 00:42
  • Those still exist as variables, you can just use them... – Doyousketch2 Nov 29 '20 at 00:43
  • I know, I just need them as strings so I can later paste the string into a file name. (My real data has a lot more entries, I will need many different file names with a changing string in them, so I need a systematic method). – cmoez Nov 29 '20 at 00:45
  • I'm still not certain why you need the variable name as a string. You can format its contents to fit whatever desired output is required. `f'{ urllist [0] [1] }'` – Doyousketch2 Nov 29 '20 at 00:58