This question already has answers here:
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
Closed 10 years ago.
I want convert list as follow:
list=[['a','b','c','d'],'e','f']
to
list['a','b','c','d','e','f']
how could I do this....Helples..
Check out itertools.chain, I think it's exactly what you need: http://docs.python.org/2/library/itertools.html#itertools.chain
>>> import itertools as it
>>> li = [['e', 'f', 'g'], 'a', 'b']
>>> list(it.chain.from_iterable(li))
['e', 'f', 'g', 'a', 'b']
This is pretty much the example from the documentation of that function, which is always a good place to start...
Related
This question already has answers here:
Python -Intersection of multiple lists?
(6 answers)
Closed 3 years ago.
Is there a way to avoid this for loop in favor of efficiency? I was thinking about iter/next functions but they don't seem to work properly..
def foo():
lst = [['a', 'b', 'c', 'd'], ['d', 'e', 'f', 'g'], ['d', 'h', 'i', 'j']]
res = set(lst[0])
for word in lst:
res = res.intersection(word)
return ''.join(res)
set.intersection isn't limited to a single argument.
res = set(lst[0]).intersection(*lst[1:])
For example:
>>> foo = set([1,2,3])
>>> foo.intersection([1,2])
{1,2}
>>> foo.intersection([2,3])
{2,3}
>>> foo.intersection([1,2], [2,3])
{3}
This question already has answers here:
How do I find the duplicates in a list and create another list with them?
(42 answers)
Closed 3 years ago.
I have a list with several stings, with some being duplicates. I need to pull out all the duplicate strings and append them into a new list. How can I do that?
list_i = ['a','b','a','c','a','c','g','w','s','c','d','a','b','c','a','e']
Use an OrderedDict to get a list without the duplicates then remove those from a copy of the original
from collections import OrderedDict
list_i = ['a','b','a','c','a','c','g','w','s','c','d','a','b','c','a','e']
non_dupes = list(OrderedDict.fromkeys(list_i))
dupes = list(list_i)
for d in non_dupes:
dupes.remove(d)
print(dupes)
#['a', 'a', 'c', 'c', 'a', 'b', 'c', 'a']
print(non_dupes)
#['a', 'b', 'c', 'g', 'w', 's', 'd', 'e']
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
Closed 7 years ago.
For example:
lst = [('ABC','DEF'),('HIJ','KLM')]
To get:
>>> ['A','B','C','D','E','F','G','H','I','J','K','L','M']
You can use a nested list comprehension :
>>> [t for i in lst for word in i for t in word]
['A', 'B', 'C', 'D', 'E', 'F', 'H', 'I', 'J', 'K', 'L', 'M']
This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 8 years ago.
I have a list in python that looks like that: [a,b,c,d,e,f,g,h,i]. I would like to convert this list to a list of lists (nested list). The second level of lists should contain four elements maximal. Therefore, the new list should look like that:
[
[a,b,c,d],
[e,f,g,h],
[i],
]
Is there a pythonic way to do this? I will have to do this several times so I would be pleased if anybody knows a way of doing this without using hundreds of indexes.
You can use a list comprehension, xrange, and Explain Python's slice notation:
>>> lst = ['a', 'b', 'c', 'd', 'e',' f', 'g',' h', 'i']
>>> n = 4 # Size of sublists
>>> [lst[x:x+n] for x in xrange(0, len(lst), n)]
[['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i']]
>>>
This question already has answers here:
How do I sort a dictionary by value?
(34 answers)
Closed 8 years ago.
I have a small script in Python with a dictionary same as follows:
d = {'category_1' : ['a', 'b'],
'category_2' : ['c', 'd', 'e'],
'category_3' : ['z']}
How can I sort it based on number of values in list? I want it looks like:
d = {'category_3' : ['z'],
'category_1' : ['a', 'b'],
'category_2' : ['c', 'd', 'e']}
Dictionaries in Python are orderless.
In order to actually store ordering, you will need to either have a list of tuples, or use a collections.OrderedDict().
>>> from collections import OrderedDict
>>> OrderedDict(sorted(d.items(), key=lambda item: len(item[1])))
OrderedDict([('category_3', ['z']), ('category_1', ['a', 'b']), ('category_2', ['c', 'd', 'e'])])
The ordering is achieved here by using the sorted() built-in, with a simple key function.