Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Iteration Help
#1
I've got a list [my_list] that contains a variable part of a web address I want to access. The web address outputs a result that I want to grab with .get and add into a new list.

Example:
www.list.com/my_list[0]

Output from site:
xyz

Ideally I'd like to create a new list with xyz in the same position as the variable part of the address in the first list.

I've got thousands of items in the list, so looking to get away from doing this manually, but my novice brain hasn't put together how.

Thanks in advance.
Reply
#2
Can we see the code you are using?
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Use zip() on the lists.
Also add dict() would be natural for this.
lst1 = ['www.list.com/my_list1', 'www.list.com/my_list2', 'www.list.com/my_list3']
lst2 = ['xyz', 'abc', '123']
>>> zip(lst1, lst2)
<zip object at 0x0000022FF9EF4EC0>
>>> list(zip(lst1, lst2))
[('www.list.com/my_list1', 'xyz'),
 ('www.list.com/my_list2', 'abc'),
 ('www.list.com/my_list3', '123')]

>>> d = dict(zip(lst1, lst2))
>>> d
{'www.list.com/my_list1': 'xyz',
 'www.list.com/my_list2': 'abc',
 'www.list.com/my_list3': '123'}
>>> 
>>> d.get('www.list.com/my_list1')
'xyz'

>>> inv_dict = {v: k for k, v in d.items()}
>>> inv_dict
{"xyz": "www.list.com/my_list1",
"abc": "www.list.com/my_list2",
"123": "www.list.com/my_list3"}
>>>
>>> inv_dict.get('xyz')
'www.list.com/my_list1'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list call problem in generator function using iteration and recursive calls postta 1 1,922 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 2,794 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  issue with updating list every iteration of a loop ftrillaudp 2 3,075 Oct-29-2018, 03:23 AM
Last Post: ftrillaudp
  dictionary, list and iteration Annie 2 3,752 Jan-08-2017, 10:44 AM
Last Post: Annie

Forum Jump:

User Panel Messages

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