Add Variables to Tuple
Tuples are immutable; you can't change which variables they contain after construction. However, you can concatenate or slice them to form new tuples:
a = (1, 2, 3)
b = a + (4, 5, 6) # (1, 2, 3, 4, 5, 6)
c = b[1:] # (2, 3, 4, 5, 6)
And, of course, build them from existing values:
name = "Joe"
age = 40
location = "New York"
joe = (name, age, location)
Efficient way to add elements to a tuple
If you are only adding a single element, use
a += (5, )
Or,
a = (*a, 5)
Tuples are immutable, so adding an element will mean you will need to create a new tuple object. I would not recommend casting to a list unless you are going to add many elements in a loop, or such.
a_list = list(a)
for elem in iterable:
result = process(elem)
a_list.append(result)
a = tuple(a_list)
If you want to insert an element in the middle, you can use:
m = len(a) // 2
a = (*a[:m], 5, *a[m:])
Or,
a = a[:m] + (5, ) + a[m:]
Python add item to the tuple
You need to make the second element a 1-tuple, eg:
a = ('2',)
b = 'z'
new = a + (b,)
python: multiple variables using tuple
The way you used the tuple was only to assign the single values to single variables in one line. This doesn't store the tuple anywhere, so you'll be left with 4 variables with 4 different values. When you change the value of country, you change the value of this single variable, not of the tuple, as string variables are "call by value" in python.
If you want to store a tuple you'd do it this way:
tup = ('Diana',32,'Canada','CompSci')
Then you can access the values via the index:
print tup[1] #32
Edit:
What I forgot to mention was that tuples are not mutable, so you can access the values, but you can't set them like you could with arrays.
You can still do :
name, age, country, job = tup
But the values will be copies of the tuple - so changing these wont change the tuple.
Inserting an item in a Tuple
You can cast it to a list, insert the item, then cast it back to a tuple.
a = ('Product', '500.00', '1200.00')
a = list(a)
a.insert(3, 'foobar')
a = tuple(a)
print a
>> ('Product', '500.00', '1200.00', 'foobar')
Add Variables to Tuple Using list looping
Python discourages using indices this way unless you really need them. When you write for i in t:
, i
will be the values of t
not the indices, so t[i]
is probably not what you want — that will be t[10]
, t[20]
, etc...
The pythonic way is to use a comprehension:
t=(10,20,30,40,50)
t = tuple(n + 5 for n in t)
print(t)
# (15, 25, 35, 45, 55)
If you really want to use a loop, you can just append to the list as you go:
t=(10,20,30,40,50)
lst = []
for n in t:
lst.append(n+5)
t = tuple(lst)
print(t)
# (15, 25, 35, 45, 55)
How to add elements of a list by element to a tuple?
In python 3 you can unpack listy
into the tuple as:
items.append((var1, var2, var3, *listy))
In python 2 you'll need to do an explicit add:
items.append((var1, var2, var3) + tuple(listy))
Related Topics
Load CSV Data into MySQL in Python
Moving X-Axis to the Top of a Plot in Matplotlib
Using Colormaps to Set Color of Line in Matplotlib
Find the Most Frequent Number in a Numpy Array
How to Change the Range of the X-Axis with Datetimes in Matplotlib
Solving "Dll Load Failed: %1 Is Not a Valid Win32 Application." for Pygame
How to Print a List in Python "Nicely"
Dummy Variables When Not All Categories Are Present
Pil: Convert Bytearray to Image
Why Are 0D Arrays in Numpy Not Considered Scalar
Formatting Long Numbers as Strings in Python
What Do I Do When I Need a Self Referential Dictionary