With this tuple
x = ('A','B','C')
you can get a tuple containing all but the first element using a slice:
x[1:]
Result:
('B','C')
Answer from khelwood on Stack Overflowpython - Removing first elements of tuples in a list - Stack Overflow
pop/remove items out of a python tuple - Stack Overflow
python - How to remove the first instance of an element in a tuple? - Stack Overflow
python - How to remove the first element from a list of tuples? - Stack Overflow
Firstly tuple is immutable.
Secondly try this approach using a list comprehension:
a_list = [el[1:] for el in values]
Check slice notation.
You could use the following list comprehension if tuples are not required:
a_list = [a_tuple[1:] for a_tuple in values]
print(a_list)
This would print the following:
[('hi', 'you'), (' bye', 'bye')]
As DSM mentions, tuple's are immutable, but even for lists, a more elegant solution is to use filter:
tupleX = filter(str.isdigit, tupleX)
or, if condition is not a function, use a comprehension:
tupleX = [x for x in tupleX if x > 5]
if you really need tupleX to be a tuple, use a generator expression and pass that to tuple:
tupleX = tuple(x for x in tupleX if condition)
Yes we can do it. First convert the tuple into an list, then delete the element in the list after that again convert back into tuple.
Demo:
my_tuple = (10, 20, 30, 40, 50)
# converting the tuple to the list
my_list = list(my_tuple)
print my_list # output: [10, 20, 30, 40, 50]
# Here i wanna delete second element "20"
my_list.pop(1) # output: [10, 30, 40, 50]
# As you aware that pop(1) indicates second position
# Here i wanna remove the element "50"
my_list.remove(50) # output: [10, 30, 40]
# again converting the my_list back to my_tuple
my_tuple = tuple(my_list)
print my_tuple # output: (10, 30, 40)
Thanks
The reason this isn't as straightforward as one might think is that tuples are immutable.
An easy way would be to convert to list, use remove to remove the first instance, and convert back to tuple:
t = (7, 5, 3, 3, 6, 3, 9)
t2 = list(t)
t2.remove(3)
tuple(t2)
# (7, 5, 3, 6, 3, 9)
As a Function:
def takeOut(k, r):
k2 = list(k)
k2.remove(r)
return tuple(k2)
takeOut((7, 5, 3, 3, 6, 3, 9), 3)
# (7, 5, 3, 6, 3, 9)
I'd go with slicing:
def remove_element(seq, element):
try:
index = seq.index(element)
return seq[:index] + seq[index + 1:]
except ValueError: # element doesn't exist
return seq
A tuple in python is an unchangeable object. You can store anything you want in it but once it is declared you cannot change it back.
Here a link to tuples : https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences (section 5.3)
The popfunction deletes the last element from your list and returns it. Since you have tuples in your list, calling the pop function on your list of tuples will only result in returning and deleting the last tuple from your list.
Tuples are immutable, so you can't change them. You can however overwrite your list with a list of new tuples:
x = [('a','b','c'), ('d','e','f'), ('g','h','i'), ('j','k','l')]
x = [tpl[1:] for tpl in x]
Output:
[('b', 'c'), ('e', 'f'), ('h', 'i'), ('k', 'l')]