This question already has answers here:
Is there a simple way to delete a list element by value?
(25 answers)
Closed 7 years ago.
How to find and delete only one element in a list in Python?
# Example:deleting only the first (1,2) in the list
a = [(4, 5), (1, 2), (7, 5), (1, 2), (5, 2)]
# result expected
a = [(4, 5), (7, 5), (1, 2), (5, 2)]
Use the list.remove() method to remove the first occurrence, in-place:
a.remove((1, 2))
Demo:
>>> a = [(4, 5), (1, 2), (7, 5), (1, 2), (5, 2)]
>>> a.remove((1, 2))
>>> a
[(4, 5), (7, 5), (1, 2), (5, 2)]
See the Mutable Sequence Types documentation:
s.remove(x)
same as del s[s.index(x)]
and s.index() only ever finds the first occurrence.
Hello if you want to delete any thing in lists
use these codes
mylist.pop(element_order) #mylist stands for your list and
#element_order stands for the order of element is the list and if it is the
#first element it will be 0
or you can use
mylist.remove(the_element)
note that in the pop it is a method not a list
mylist.pop(0)
print mylist
dont use
mylist = mylist.pop(0)
Related
This question already has answers here:
Sort a list of tuples by 2nd item (integer value) [duplicate]
(9 answers)
Closed 1 year ago.
I am currently trying to solve a problem involving lists and sorting (i.e.) when a list (not a normal one ,but a list of tuple ) is entered ; the program should print out the list but in an orderly(increasing order) manner based on the 2nd elements of each tuples.
ex:
Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Expected Result : [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
Here is what I have tried until now:
def sorting(L):
le=len(L)
G=[]
Lnew=[list(l) for l in L]
for i in range(le):
G.append(Lnew[i][1])
G.sort()
Lnew.remove(Lnew[i][1]) #where the problem is
for k in range(len(G)):
Lnew[k][1]=G[k]
for elt in Lnew:
tuple(elt)
return L
An error displays "list.remove(x): x not in list" .
how do I proceed in that case? or is there a simpler way to tackle the problem ?
from operator import itemgetter
lista_of_tuples = sorted(list_of_tuples, key_itemgetter(0))
itemgetter parameter can be 0,1,2 depending which one to order it by
def sorting(my_list):
return sorted(my_list, key=lambda x: x[1])
sorting([(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)])
Output: [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
I want to convert list = [1,4,2,3,0] to list_tup = [(1,4),(4,2),(2,3),(3,0)]. You can see my code below but it outputs [(1,4),(2,3)]. I am wondering how to adjust indices in zip.
list=[1,4,2,3,0]
list_tup = tuple(zip(list[0::2], list[1::2]))
Try zipping the whole list with the list without the first element:
l = [1,4,2,3,0]
print(list(zip(l, l[1:])))
Or use unpack *:
l = [1,4,2,3,0]
print([*zip(l, l[1:])])
They both output:
[(1, 4), (4, 2), (2, 3), (3, 0)]
Try Zipping whole list without first element of the list
l = [1,4,2,3,0]
print(list(zip(l, l[1:])))
output:
[(1, 4), (4, 2), (2, 3), (3, 0)]
Here is a simple for loop through an enumerate object. This terminates due to (this line I have mentioned as a comment). Why is that?
enum_arr = enumerate(arr)
for ele in enum_arr:
print(ele)
print(list(enum_arr)[ele[0]:]) # terminates due to this line
Output:
(0, 0)
[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
If I comment out the second print statement, then:
Output:
(0, 0)
(1, 1)
(2, 2)
(3, 3)
(4, 4)
(5, 5)
As expected.
Why is this happening?
enumerate() gives you an iterator object. Iterators are like a bookmark in a book that can only be moved forward; once you reach the end of the book you can't go back anymore, and have to make a new bookmark.
You then use that iterator in two places; the for loop and list(). The list() function moved the bookmark all the way to the end, so the for loop can't move it any further.
You'd have to create a new enumerate() object in the loop if you want to use a separate, independent iterator:
enum_arr = enumerate(arr)
for ele in enum_arr:
print(ele)
print(list(enumerate(arr[ele[0]:], ele[0])))
This does require that arr is itself not an iterator, it has to be a sequence so you can index into it. I'm assuming here that you have a list, tuple, range or similar value.
Note that I passed in ele[0] twice, the second argument to enumerate() lets you set the start value of the counter.
It is easier to use a tuple assignment here to separate out the count and value:
for count, value in enum_arr:
print((count, value))
print(list(enumerate(arr[count:], count)))
Demo:
>>> arr = range(6)
>>> enum_arr = enumerate(arr)
>>> for count, value in enum_arr:
... print((count, value))
... print(list(enumerate(arr[count:], count)))
...
(0, 0)
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
(1, 1)
[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
(2, 2)
[(2, 2), (3, 3), (4, 4), (5, 5)]
(3, 3)
[(3, 3), (4, 4), (5, 5)]
(4, 4)
[(4, 4), (5, 5)]
(5, 5)
[(5, 5)]
Coming back to the book analogy, and the requirement that arr is a sequence: as long as arr is a book with page numbers, you can add more bookmarks at any point. If it is some other iterable type, then you can't index into it and so would have to find some other means to 'skip ahead' and back again. Stretching the analogy further: say the book is being streamed to you, one page at a time, then you can't go back once you received all the pages. The solution coud be to create a local cache of pages first; if you can spare the memory that could be done with cached_copy = list(arr). Just take into account that you have to be sure that the book you are receiving is not so long as to require more space than you actually have. And some iterables are endless, so would require infinite memory!
Let's say I have a list like so: [(1, (1,2)), (2, (23, -10)), (3, (4, 5))...]
I want to get (1,2), (23, -10), etc
edit: Thanks for help. I didn't know about list comprehension as I'm not too familiar with python
Try Something like this:-
Here is List and get other list of tuples:-
a = [(1, (1,2)), (2, (23, -10)), (3, (4, 5))...]
b = map(lambda item: item[1], a)
print b
This will solve your problem.
Here is list of tuples where second elem of each tuple is also a tuple. To get that second elem we are going to use lambda that is going to take elements from list and just return second item from that element, in this case being desired tuple. The map function also creates a list of returned values.
>>> list_of_nested_tuples = [(1, (1, 2)), (2, (23, -10)), (3, (4, 5))]
>>> b = map(lambda item: item[1], list_of_nested_tuples)
>>> b
[(1, 2), (23, -10), (4, 5)]
Take note that it would be more clear to just use list comprehension like so
>>> [elem[1] for elem in list_of_nested_tuples]
[(1, 2), (23, -10), (4, 5)]
Yes, you can iterate over all tuples and then take the second element:
list = [(1, (1,2)), (2, (23, -10)), (3, (4, 5))]
for elem in list:
print(elem[1])
In each iteration elem value its (1,(1,2)) -> (2,(23,-10)) -> ....
Then you take the second item of the tuple (index 1)
This question already has an answer here:
Python += with a list and a tuple [duplicate]
(1 answer)
Closed 7 years ago.
<class 'list'>'s insert method helps to add element ((1,2), (3, 4)) in a lst with expected output lst = [((1,2), (3, 4))],
lst = []
lst.insert(0, ((1,2), (3, 4)))
lst.insert(1, ((5, 6), (7, 8)))
But += operator does not give same result(lst = [(1, 2), (3, 4)]).
lst = []
lst += ((1,2), (3, 4))
+= operator internally calls __iadd__ method.
Why __iadd__ does not add element in a way class <'list'>'s insert(index,element) method does ? Because maintaining index needs extra name-value pair in my merge sort code is a big overhead..
x += y, where x and y are list-like will create a new list with the contents of y concatenated to the contents of x.
Since you don't want to append the contents of the tuple ((1,2), (3, 4)) to lst, instead want the tuple added to the list, you should lst.append(((1,2), (3, 4))) instead.
>>> lst = []
>>> lst.append(((1, 2), (3, 4)))
>>> lst.append(((5, 6), (7, 8)))
>>> lst
[((1, 2), (3, 4)), ((5, 6), (7, 8))]