python - How do I append tuples together? -


i'm trying put multiple tuples not know how. know how create tuple, i'm not quite sure how put them together. want keep on appending (not appending because don't want list). i'm pulling string out of each line , putting of tuples

x = (132, 534, 4) y = (345, 531, 1) z = (212, 421, 5) 

what want returned

(132, 534, 4), (345, 531, 1), (212, 421, 5) 

the tuple in example can created this:

>>> # following line equivalent to:  new_tuple = (x, y, z) >>> new_tuple = x, y, z >>> new_tuple ((132, 534, 4), (345, 531, 1), (212, 421, 5)) >>> 

however, because tuples immutable (cannot changed after creation) sequences, need create new 1 each time want "append" it:

>>> new_tuple = x, y, z >>> new_tuple ((132, 534, 4), (345, 531, 1), (212, 421, 5)) >>> w = 1, 2, 3  # tuple needs go inside new_tuple >>> new_tuple = x, y, z, w  # so, have rebuild new_tuple include >>> new_tuple ((132, 534, 4), (345, 531, 1), (212, 421, 5), (1, 2, 3)) >>> 

this why best use list, mutable (can changed after creation) sequence has append method:

>>> new_list = [x, y, z]  # square brackets make list >>> new_list [(132, 534, 4), (345, 531, 1), (212, 421, 5)] >>> w = 1, 2, 3 >>> new_list.append(w)  # add w new_list without rebuilding >>> new_list [(132, 534, 4), (345, 531, 1), (212, 421, 5), (1, 2, 3)] >>> 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -