Question:
Matching Lists in Python?
2012-04-17 09:34:16 UTC
I need to pair up two different lists in Python 3. For example, if I had ['1','2','3'] and ['X','Y','Z'] the output would return something like the pairs 1,X and 2,Y and so on. Sorry I'm really new to python.
I'm not necessarily looking for a direct answer, any guidance would be appreciated :) thanks!
Three answers:
2012-04-17 10:14:15 UTC
Since you need corresponding elements from two different arrays, then you will need to get each element by position. First you need the length of the array, and then iterate up to the length.



a = ['1','2','3']

b = ['X','Y','Z']



for i in range(len(a)): print '%s,%s' % (a[i], b[i])



In this case, len(a) gets the length of the first array. We are assuming the second array is the same length. The 'range' function converts the length into a sequence of integers, and the variable 'i' will be used in the for loop for each integer value. The integers range from 0 to length-1. These are indexes used to get the values from arrays a and b.



The print statement uses a formatted string '%s,%s' to place the two values from a[i] and b[i].



Another thing you can do is create a new array with the pairs of values. You can create a list of tuples as follows:



k=[]

for i in range(len(a)): k.append((a[i], b[i]))

print k



In this case, you append each pair (a[i],b[i]) to the list k.
colmenero
2016-11-30 09:51:28 UTC
i'm no longer astounding myself, and don't comprehend python, yet i'm going to a minimum of attempt to describe what a string is. A string is a command, or a sequence of training engaged on an identical time, to end one particular interest. A string is many cases bearing on a minimum of one line of code. as an social gathering, in a batch report the command initiate C:residing house abode windows will open the folder with the take care of "C:residing house abode windows" this would in lots of circumstances be suggested as a string. training use strings with the aid of actuality there is not any thank you to no longer use them. In, say, Microsoft be unsleeping... there is in all probability a string that publicizes as quickly as you press the "a" key, an "a" shows up on the exhibit exhibit screen. yet yet another set of strings ought to administration the font, and so on and so on. optimal strings you come back for the time of will in all probability be absolute training (they do no longer decide for some thing exterior, such as you pressing a key, to turn on them...) or what I call "If-Thens" which surely capacity "If" you press a key (or do some thing else...) "Then" some thing will ensue. It gets so plenty extra desirable complicated than that contained for the period of a finished blown utility, like, say Firefox. Strings will place self perception in a single yet yet another and set one yet yet another off... in accordance to possibility i'm rambling too plenty, yet I hopr this permits... no remember if this is in basic terms a sprint....
Thomas Bladen-Hovell
2012-04-17 10:14:15 UTC
All you need to do is to print the same list item from each array:



>>> list1 = ['1', '2', '3']

>>> list2 = ['x', 'y', 'z']

>>> for i in range(len(list1)):

............print '%s, %s' %(list2[i], list1[i])





1, x

2, y

3, z


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...