This question already has answers here:
Remove all occurrences of a value from a list?
(26 answers)
Closed 4 years ago.
How can I remove several same items in a LIST
for ex:
a = [1,1,1,1,1,1,2,2,2,2,2,2]
I want to remove all 1 values, such that output is:
a = [2,2,2,2,2,2]
I tried a.remove(1) but it only remove one '1' at the first encounter.
i try looking for comprehension method
Try it with a list comprehension:
a = [1,1,1,1,1,1,2,2,2,2,2,2]
cleaned_a = [el for el in a if el != 1]
Related
This question already has an answer here:
How to zip lists in a list [duplicate]
(1 answer)
Closed 3 years ago.
I have a list
mainlist=[[1,2,3,4],['a','b','c','d'],[4,5,6,7]]
want to make sublist based on index position in sublist. The output is
finallist=[[1,'a',4],[2,'b',5],[3,'c',6],[4,'d',7]]
Thanks
Use zip with unpacking:
finallist = list(map(list, zip(*mainlist)))
This question already has answers here:
How to create a number of empty nested lists in python [duplicate]
(2 answers)
Closed 3 years ago.
I am trying to create a nested list. The result would be something like [[0,1],[2,3],[0,4]] I tried the following and got an index out of range error:
list = []
list[0].append(0)
Is it not appending 0 to the first item in the list? How should I do this? Many thanks for your help.
A little typo, you should do:
list = [[]]
list[0].append(0)
You need to have a first element first...
Edit:
Use:
list = []
for i in range(3):
list.append([])
list[-1].append(0)
For that you'll need to append a list to a list first, i.e.:
list = []
list.append([])
list[0].append(0)
print(list)
# [[0]]
lst = []
lst.append([0,1])
lst.append([2,3])
lst.append([0,4])
print(lst)
How about this ? Or you need in the form of loop with constant set of numbers?
This question already has answers here:
Finding and replacing elements in a list
(10 answers)
Closed 4 years ago.
I would like to update an element from a list.
This is my kind of list:
list= ["toto", "stack" , "element_to_update", "overflow"]
I want to update this list when "toto" and "stack" found as element[0] and element[1] to have my list updated (and concatenate with existing element[2])
The list that I want at the end:
list=["toto", "stack", "element_to_update_is_updated", "overflow"]
What is the best way to do that?
Thanks in advance for your help.
Why not:
l = ["toto", "stack", "element_to_update", "overflow"]
if l[:2] == ['toto','stack']:
l[2] += '_is_updated'
And now:
print(l)
Is:
['toto', 'stack', 'element_to_update_is_updated', 'overflow']
This question already has answers here:
Removing duplicates in lists
(56 answers)
Closed 5 years ago.
I have a list of strings in which there are a lot of repeated items. I would like to make a new list in which all items are present but there is only one occurrence of each item.
input:
mylist = ["hg", "yt", "hg", "tr", "yt"]
output:
newlist = ["hg", "yt", "tr"]
I actually have tried this code but did not return what I want:
newlist = []
for i in range(len(mylist)):
if mylist[i+1] == mylist[i]:
newlist.append(mylist[i])
You can simply use a set:
newlist = set(mylist)
Or, to retrieve exactly a list, but is can be useless depending what you are doing with:
nexlist = list(set(mylist))
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 7 years ago.
Assume you have a list :
mylist=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
any pythonic(2.x) way to unpack the inner lists so that new list should look like ?:
mylist_n=[1,2,3,4,2,3,4,5,3,4,5,6]
import itertools
mylist=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
print list(itertools.chain(*mylist))
mylist_n = [j for i in mylist for j in i]