Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Iteration Help
#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


Messages In This Thread
List Iteration Help - by javeeva - Feb-07-2022, 07:52 PM
RE: List Iteration Help - by menator01 - Feb-07-2022, 08:19 PM
RE: List Iteration Help - by snippsat - Feb-07-2022, 11:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  list call problem in generator function using iteration and recursive calls postta 1 1,962 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 2,848 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  issue with updating list every iteration of a loop ftrillaudp 2 3,106 Oct-29-2018, 03:23 AM
Last Post: ftrillaudp
  dictionary, list and iteration Annie 2 3,793 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