Combine Python lists with similar elements [duplicate] - python

This question already has answers here:
Interleave different length lists, elimating duplicates, and preserve order
(7 answers)
Closed 1 year ago.
I'd like to merge to lists with many similar elements preserving the order as much as possible.
So say for example we have this:
list1=[1,2,3,4,6,7,8,9]
list2=[1,2,3,4,5,8,10]
list1+list2=[1,2,3,4,6,5,7,8,9,10]
or maybe:
list1+list2=[1,2,3,4,5,6,7,8,10,9]
The idea is that 1,2,3,4 all match. Then we encounter 5 in list1 and 6 in list2 and we insert them next to each other in the merge list. If there are more elements in one list than the other then the extra rows are just appended to the end.

For unsorted output-
list(set(list1+list2))
For sorted output-
list(sorted(set(list1+list2)))

Related

Finding the number of combinations possible, given 4 dictionaries [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 3 months ago.
Given the following dictionaries:
dict_first_attempt = {'Offense': ['Jack','Jill','Tim'],
'Defense':['Robert','Kevin','Sam']}
dict_second_attempt = {'Offense': ['Jack','McKayla','Heather'],
'Defense':['Chris','Tim','Julia']}
From this dictionaries, my focus is just the offense, so if I just wanted the list of those, I would do this:
first = dict_first_attempt['Offense']
second = dict_second_attempt['Offense']
For each of those lists, I am trying to create a code that can do the following:
Tell me all the possible combinations of first attempt offense and second attempt offense.
Outputs it in a list, with lists of the combinations.
The first element within the list has to be from the first attempt offense, and the second element has to be from the second attempt offense.
An example of the type of output I want is:
[['Jack','Jack'],['Jack','McKayla'],['Jack','Heather'],
['Jill','Jack'],['Jill','McKayla'],['Jill','Heather'],
['Tim','Jack'],['Tim','McKayla'],['Tim','Heather']]
import itertools
list(itertools.product(first, second))

get list of lists from dict of lists by grouping elements at same index in every key-value pair [duplicate]

This question already has an answer here:
zip two values from the dictionary in Python
(1 answer)
Closed 10 months ago.
I have a dictionary
a = {'a':[1,2,3],'b':[4,5,6]}
Now, I wish to convert it into a list of lists such that
[[1,4],[2,5],[3,6]]
i.e. the 1st element of every key-value pair grouped together, every 2nd element grouped together & likewise. Also, number of keys isn't restricted to 2 & can be 'n'
If you're fine with the results being tuples rather than lists an easy way is:
list(zip(*a.values()))
else sprinkling in some list comprehension can cast to the correct type:
[list(value_pair) for value_pair in zip(*a.values())]

How can I split a list into smaller lists [duplicate]

This question already has answers here:
How can I find same values in a list and group together a new list?
(6 answers)
Closed 2 years ago.
I have a list like so:
[10,10,10,20,20,20,20,30,40,40,40]
I want to split into X amount of lists, where X = how many unique elements there are, in the case above there are 4. So I would want 4 lists like so:
[[10,10,10],[20,20,20,20],[30],[40,40,40]]
Might be a dumb question and there is an easy way to do this but any help is appreciated, language is python3.
itertools.groupby does what you need, except it returns iterators instead of lists. Converting to lists is easy though:
[list(g) for _, g in itertools.groupby(my_list)]

How to chain individual elements of list into one individual element in Python [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
I have the following list
List1 =['4','0','1','k']
How do i make sure that individual elements are combined into one single entity?
Here is the desired output
List1 =['401k']
Use str.join:
List1 = [''.join(List1)]

How to build a third list by comparing two other list with string matching. [duplicate]

This question already has answers here:
Find intersection of two nested lists?
(21 answers)
Closed 6 years ago.
The answer to my question is probably somewhere around here but I couldn't find it.
I have a two lists :
['batman','superman','spiderman',]
['batman','ironman','superman','flash','wonderwoman']
I want to compare the two lists and return the matching elements as a third list :
['batman','superman']
I only found this solution:
list=['a cat','a dog','a yacht']
string='a cat'
if string in list:
print 'found a cat!'
But it's only a comparison between a string and a list...
Use intersection,
l1 = ['batman','superman','spiderman',]
l2 = ['batman','ironman','superman','flash','wonderwoman']
print(set(l1).intersection(set(l2)))
#set(['batman', 'superman'])

Categories