In: Computer Science
suppose i have a list in python that is [hello,yo,great,this,cool,fam]
how do I get all the possible 2 combination that I can have from this list in tuples for example output
[ {hello,hello},{hello,yo},{hello,great},....... {yo,hello},{yo,yo} and so on and son}
no packages allowed
s=[]
l=[]
l2=[]
print("Enter the size of the list :")
n=int(input())
print("Enter the elements in the list ")
for i in range(0,n):
l.append((input()))
for i in range(0,n):
j=0
for j in range(0,n):
s=[]
s.append(l[i])
s.append(l[j])
l2.append(tuple(s))
print(l2)
The above program should be used when you want a user input but not when list is to be pre defined.It will asks the user to enter size and then elements of the list and then print the list of tuples exactly as defined in the program.
SCREENSHOT OF THE OUTPUT :
PYTHON CODE FOR PREDEFINED LIST.
s=[]
l=['hello','yo','great','this','cool','fam']
l2=[]
for i in range(0,len(l)):
j=0
for j in range(0,len(l)):
s=[]
s.append(l[i])
s.append(l[j])
l2.append(tuple(s))
print(l2)
SCREENSHOT OF THE OUTPUT :