Python | Equate two list index elements

Sometimes we need to link two list from the point of view of their index elements and this kind of problem comes mostly in places where we need to display in formatted form the linkage of two lists with one another. This a very specific problem, but can be useful whenever we need a possible solution. Let’s discuss certain ways in which this can be done.

Method #1 : Using formatting + tuple()
The string formatting can be used to specify the way we need to output the list and the task of pairing the like indices can be done with the help of tuple function.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Equate two list index elements
# using formatting + tuple()
  
# initializing lists 
test_list1 = ['GeeksforGeeks', 'is', 'best']
test_list2 = ['1', '2', '3']
  
# printing original lists 
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
  
# using formatting + tuple() to
# Equate two list index elements
temp = len(test_list1) * '% s = %% s, '
res = temp % tuple(test_list1) % tuple(test_list2)
  
# printing result
print ("The paired elements string is : " + res)

chevron_right


Output :



The original list 1 is : ['GeeksforGeeks', 'is', 'best']
The original list 2 is : ['1', '2', '3']
The paired elements string is : GeeksforGeeks = 1, is = 2, best = 3, 

 

Method #2 : Using join() + zip()
These two methods can also combine to achieve this particular task, the join function can be used to extend the format logic to all indices and construct a string, zip function pairs the like index elements from both the tuples.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Equate two list index elements
# using join() + zip()
  
# initializing lists 
test_list1 = ['GeeksforGeeks', 'is', 'best']
test_list2 = ['1', '2', '3']
  
# printing original lists 
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
  
# using join() + zip() to
# Equate two list index elements
res = ', '.join('% s = % s' % i for i in zip(test_list1, test_list2))
  
# printing result
print ("The paired elements string is : " + res)

chevron_right


Output :

The original list 1 is : ['GeeksforGeeks', 'is', 'best']
The original list 2 is : ['1', '2', '3']
The paired elements string is : GeeksforGeeks = 1, is = 2, best = 3, 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.