This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 2 years ago.
How to flatten the coordinates?
For input :[(1,2),(5,6),(7,8)]
expecting the output as [1,2,5,6,7,8]
Try it like this:
mylist = [(1,2),(5,6),(7,8)]
newlist = []
for x in mylist:
newlist += x
print(newlist)
from the itertools doc page
from itertools import chain
def flatten(listOfLists):
"Flatten one level of nesting"
return chain.from_iterable(listOfLists)
Related
This question already has answers here:
Removing duplicates in lists
(56 answers)
Closed 2 years ago.
How can I remove duplicates from this list?
x = [name, code]
Some elements of list has the same code:
list = [['cafe', '/fr/48349066'], ['cafe', '/fr/48349056'], ['cafe', '/fr/48349066']]
There's probably a more processing-efficient way to do this, but this is the way I've always done stuff like this:
for i in range(len(list)):
for j in range(len(list)):
if list[i][1] == list[j][1]:
list.pop(i)
There is a lot of ways to removing duplicates;
1-) The Naive Method;
new_list = []
for i in some_list:
if i not in new_list:
res.append(i)
2-) Using list comprehension;
new_list = []
[new_list.append(x) for x in some_list if x not in new_list]
3-) Using set();
some_list = list(set(some_list))
4-) Using collections.OrderedDict.fromkeys()
from collections import OrderedDict
new_list = list(OrderedDict.fromkeys(some_list))
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 6 years ago.
How do I convert a list of lists to a single list?
Input:
a=[['AA'], ['AE'], ['AH'], ['AO'],]
Desired output:
['AA','AE','AH','AO']
a=[['AA'], ['AE'], ['AH'], ['AO'],]
l=[]
for i in a:
l.extend(i)
print l
you could use comprehension list:
or using map and lambda functions
a=[['AA'], ['AE'], ['AH'], ['AO'],]
# open the item [0] with generators
Newa = [x[0] for x in a]
>>>['AA', 'AE', 'AH', 'AO']
see examples: http://www.secnetix.de/olli/Python/list_comprehensions.hawk
EDIT:
for i, value in enumerate(a):
a[i] = value[0]
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]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 8 years ago.
I have a list like:
l = [[1,2,3],[4,5]]
I would like to unpack each element to make it like:
l = [1,2,3,4,5]
I have my solution here:
l = reduce(lambda x, y: x+y, l)
Anyone has other Pythonic way? Thanks.
You should use itertools methods;
from itertools import chain
l = [[1,2,3],[4,5]]
list(chain.from_iterable(l))