0

There is a list of coordinates that I want to draw lines through on the Tkinter canvas:

points = [(1,1), (2, 2), (3, 3), (2, 0)]   # etc. This is could have 100 points or more

The next step would be calling the function create_line, but it can't support list:

Canvas.create_line(1, 1, 2, 2, 3, 3)       #this works ok
Canvas.create_line(points)                 #this doesn't work

So is there an efficient way to separate the elements in a list and insert them into the function in this order? If possible, I would love to avoid using for loops.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Dave N.
  • 138
  • 1
  • 8

1 Answers1

2

You can flatten the list points with a list comprehension:

flattened = [a for x in points for a in x]

And turn the elements of that flattened list into arguments with the "*" syntax:

Canvas.create_line(*flattened)

I politely suggest that you overcome your hangup about for loops. It is pretty much impossible to write useful programs without them.

Paul Cornelius
  • 9,245
  • 1
  • 15
  • 24