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.
# 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) |
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.
# 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) |
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,
Recommended Posts:
- Python | Add list elements with a multi-list based on index
- Python | Replace elements in second list with index of same element in first list
- Python | Index of Non-Zero elements in Python list
- Python | Sort list of list by specified index
- Python list | index()
- Python | Accessing index and value in list
- Python | Separate odd and even index elements
- Python | Subgroups of i'th index size in list
- Python | Returning index of a sorted list
- Python | Check for Nth index existence in list
- Python | Initializing dictionary with list index-values
- Python | Find mismatch item on same index in two list
- Python | Initializing dictionary with list index values
- Python | Index specific cyclic iteration in list
- Python | Print list after removing element at given index
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.



