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.