How do i create a set of tuples with each tuple containing two elements? Each tuple will have an x
and y
value: (x,y)
I have the numbers 1 through 50, and want to assign x
to all values 1 through 50 and y
also 1 through 50.
S = {(1,1),(1,2),(1,3),(1,4)...(1,50),(2,1)......(50,50)}
I tried
positive = set(tuple(x,y) for x in range(1,51) for y in range(1,51))
but the error message says that a tuple only takes in one parameter. What do I need to do to set up a list of tuples?
To create a tuple in Python, use the parentheses ( ), separated by commas. The parentheses are optional. A tuple can have many elements with different data types like integer, float, list, string, etc. To create a set in Python, use the parentheses { }, separated by commas.
Tuple. Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable.
We can create a list of tuples i.e. the elements of the tuple can be enclosed in a list and thus will follow the characteristics in a similar manner as of a Python list. Since, Python Tuples utilize less amount of space, creating a list of tuples would be more useful in every aspect.
mySet = set(itertools.product(range(1,51), repeat=2))
OR
mySet = set((x,y) for x in range(1,51) for y in range(1,51))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With