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']
Related
This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 3 months ago.
so I am trying to create a list that have different list as it element
the for loop bellow will extend an element to the bag then append it to the list bag and finally remove the extended element to repeat the cyclec
the bag contains these elements ['B', 'D']
and the rf contains these elements ['C', 'A', 'G', 'E']
list_bag = []
for i in range(len(rf)) :
bag.extend(rf[i])
a= bag
list_bag.append(a)
bag.pop()
print(list_bag)
the out put I am trying to archive is this :
[['B', 'D','A'], ['B', 'D','E'], ['B', 'D','F'], ['B', 'D','C']]
but the code keep on giving me this
[['B', 'D'], ['B', 'D'], ['B', 'D'], ['B', 'D']]
any suggestion ?
You can accomplish what you want with a list comprehension.
list_bag = [bag + [item] for item in rf]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 months ago.
I have a list with the following structure:
list = ['a', ['b','c','d'], ['e','f']]
how can I create the following structure from it:
list = ['a','b','c','d','e','f']
list = ['a', ['b','c','d'], ['e','f']]
lst = [element for nested_list in list for element in nested_list]
print(lst)
Result:
['a', 'b', 'c', 'd', 'e', 'f']
This supports nested list too.
res = []
def parse(li):
if isinstance(li, list):
for a in li:
parse(a)
return
res.append(li)
my_List = ['a', ['b','c','d', ['g', 'h']], ['e','f']]
parse(my_List)
print(res)
Output:
['a', 'b', 'c', 'd', 'g', 'h', 'e', 'f']
This question already has answers here:
Unpack list into middle of a tuple
(3 answers)
Closed 4 years ago.
How would I create a list element from function call?
Not sure if this is possible, but I've tried to create a list element from a function when i create the list as i'm not sure of the elements up until runtime
So I have tried this:
>>>> def make_list_element():
return 'd, e'
If i then try to create a list and call the function at the same time :
>>>> a = ['a', 'b', 'c', make_list_element().split(", ")]
And I get:
>>> a
>>> ['a', 'b', 'c', ['d', 'e']]
How could I achieve this:
>>> a
>>> ['a', 'b', 'c', 'd', 'e']
Preferably in the same statement as I create the list.
Many thanks
In Python3, you can simply unpack the returned list like so:
a = ['a', 'b', 'c', *make_list_element().split(", ") ]
If you're on Python2, you will have to concatenate or extend the list:
a = ['a', 'b', 'c'] + make_list_element().split(", ")
or
a = ['a', 'b', 'c']
a.extend(make_list_element().split(", "))
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.
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...