Q:

python combine lists into tuple

# Basic syntax using list comprehension:
list_of_tuples = [(list_1[i], list_2[i]) for i in range(len(list_1))]

# Note, the lists have to be the same length for this to work
# Note, you can combine as many lists as you want in this way
# Note, if you want to sort the resulting list of tuples, make sure that
#	the list you want to sort on is the first list in the tuple

# Example usage:
# Make lists
list_1 = [3, 11, 7, 17, 23]
list_2 = ['a', 'series', 'of', 'useless', 'words']
list_3 = [11.7, None, False, 'sponge', 42]
# Combine into list of tuples
list_of_tuples = [(list_1[i], list_2[i], list_3[i]) for i in range(len(list_1))]
print(list_of_tuples)
--> [(3, 'a', 11.7), (11, 'series', None), (7, 'of', False), (17, 'useless', 'sponge'), (23, 'words', 42)]
# Sort tuples in reverse order using first number in each tuple
list_of_tuples.sort(reverse=True)
print(list_of_tuples)
--> [(23, 'words', 42), (17, 'useless', 'sponge'), (11, 'series', None), (7, 'of', False), (3, 'a', 11.7)]
3

New to Communities?

Join the community