Merge tuples with the same key
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
How to merge a tuple with the same key
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
and turn them into
list_2 = [("AAA", [123, 456]), ("AAW", [147, 124])]
python tuples
New contributor
add a comment |Â
up vote
8
down vote
favorite
How to merge a tuple with the same key
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
and turn them into
list_2 = [("AAA", [123, 456]), ("AAW", [147, 124])]
python tuples
New contributor
1
Related: Python: merge lists of tuples based on its values
â Aran-Fey
8 hours ago
2
all those years and not a single exact dupe... I had a good look. Maybe it's somewhere, well...
â Jean-François Fabre
7 hours ago
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
How to merge a tuple with the same key
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
and turn them into
list_2 = [("AAA", [123, 456]), ("AAW", [147, 124])]
python tuples
New contributor
How to merge a tuple with the same key
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
and turn them into
list_2 = [("AAA", [123, 456]), ("AAW", [147, 124])]
python tuples
python tuples
New contributor
New contributor
edited 16 mins ago
codeforester
15.9k73555
15.9k73555
New contributor
asked 8 hours ago
Theprofessor14
442
442
New contributor
New contributor
1
Related: Python: merge lists of tuples based on its values
â Aran-Fey
8 hours ago
2
all those years and not a single exact dupe... I had a good look. Maybe it's somewhere, well...
â Jean-François Fabre
7 hours ago
add a comment |Â
1
Related: Python: merge lists of tuples based on its values
â Aran-Fey
8 hours ago
2
all those years and not a single exact dupe... I had a good look. Maybe it's somewhere, well...
â Jean-François Fabre
7 hours ago
1
1
Related: Python: merge lists of tuples based on its values
â Aran-Fey
8 hours ago
Related: Python: merge lists of tuples based on its values
â Aran-Fey
8 hours ago
2
2
all those years and not a single exact dupe... I had a good look. Maybe it's somewhere, well...
â Jean-François Fabre
7 hours ago
all those years and not a single exact dupe... I had a good look. Maybe it's somewhere, well...
â Jean-François Fabre
7 hours ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
8
down vote
The most performant approach is to use a collections.defaultdict
dictionary to store data as an expanding list, then convert back to tuple/list if needed:
import collections
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
c = collections.defaultdict(list)
for a,b in list_1:
c[a].extend(b) # add to existing list or create a new one
list_2 = list(c.items())
result:
[('AAW', [147, 124]), ('AAA', [123, 456])]
note that the converted data is probably better left as dictionary. Converting to list again loses the "key" feature of the dictionary.
On the other hand, if you want to retain the order of the "keys" of the original list of tuples, unless you're using python 3.6/3.7, you'd have to create a list with the original "keys" (ordered, unique), then rebuild the list from the dictionary. Or use an OrderedDict
but then you cannot use defaultdict
(or use a recipe)
1
Note that this answer would not necessarily retain the order of the original list, if that is a consideration.
â blhsing
7 hours ago
true, unless you're using python 3.7. To retain the original order you'd have to create a list with the original "keys" (ordered, unique), then rebuild the list from the dictionary.
â Jean-François Fabre
7 hours ago
add a comment |Â
up vote
4
down vote
You can use a dict to keep track of the indices of each key to keep the time complexity O(n):
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
list_2 =
i =
for k, s in list_1:
if k not in i:
list_2.append((k, s))
i[k] = len(i)
else:
list_2[i[k]][1].extend(s)
list_2
would become:
[('AAA', [123, 456]), ('AAW', [147, 124])]
add a comment |Â
up vote
1
down vote
Similarly to other answers, you can use a dictionary to associate each key with a list of values. This is implemented in the function merge_by_keys
in the code snippet below.
import pprint
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
list_2 = [("AAA", [123, 456]), ("AAW", [147, 124])]
def merge_by_key(ts):
d =
for t in ts:
key = t[0]
values = t[1]
if key not in d:
d[key] = values[:]
else:
d[key].extend(values)
return d.items()
result = merge_by_key(list_1)
pprint.pprint(result)
Why are you manually creating list_2?
â codeforester
20 mins ago
add a comment |Â
up vote
1
down vote
You can create a dictionary and loop through the list. If the item present in dictionary append the value to already existing list else assign the value to key.
dict_1 =
for item in list_1:
if item[0] in dict_1:
dict_1[item[0]].append(item[1][0])
else:
dict_1[item[0]] = item[1]
list_2 = list(dict_1.items())
New contributor
It does the merge but doesn't retain the original order in the list. The result I got is:[('AAW', [147, 124]), ('AAA', [123, 456])]
- AAA should have been the first element as in the original list.
â codeforester
22 mins ago
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
The most performant approach is to use a collections.defaultdict
dictionary to store data as an expanding list, then convert back to tuple/list if needed:
import collections
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
c = collections.defaultdict(list)
for a,b in list_1:
c[a].extend(b) # add to existing list or create a new one
list_2 = list(c.items())
result:
[('AAW', [147, 124]), ('AAA', [123, 456])]
note that the converted data is probably better left as dictionary. Converting to list again loses the "key" feature of the dictionary.
On the other hand, if you want to retain the order of the "keys" of the original list of tuples, unless you're using python 3.6/3.7, you'd have to create a list with the original "keys" (ordered, unique), then rebuild the list from the dictionary. Or use an OrderedDict
but then you cannot use defaultdict
(or use a recipe)
1
Note that this answer would not necessarily retain the order of the original list, if that is a consideration.
â blhsing
7 hours ago
true, unless you're using python 3.7. To retain the original order you'd have to create a list with the original "keys" (ordered, unique), then rebuild the list from the dictionary.
â Jean-François Fabre
7 hours ago
add a comment |Â
up vote
8
down vote
The most performant approach is to use a collections.defaultdict
dictionary to store data as an expanding list, then convert back to tuple/list if needed:
import collections
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
c = collections.defaultdict(list)
for a,b in list_1:
c[a].extend(b) # add to existing list or create a new one
list_2 = list(c.items())
result:
[('AAW', [147, 124]), ('AAA', [123, 456])]
note that the converted data is probably better left as dictionary. Converting to list again loses the "key" feature of the dictionary.
On the other hand, if you want to retain the order of the "keys" of the original list of tuples, unless you're using python 3.6/3.7, you'd have to create a list with the original "keys" (ordered, unique), then rebuild the list from the dictionary. Or use an OrderedDict
but then you cannot use defaultdict
(or use a recipe)
1
Note that this answer would not necessarily retain the order of the original list, if that is a consideration.
â blhsing
7 hours ago
true, unless you're using python 3.7. To retain the original order you'd have to create a list with the original "keys" (ordered, unique), then rebuild the list from the dictionary.
â Jean-François Fabre
7 hours ago
add a comment |Â
up vote
8
down vote
up vote
8
down vote
The most performant approach is to use a collections.defaultdict
dictionary to store data as an expanding list, then convert back to tuple/list if needed:
import collections
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
c = collections.defaultdict(list)
for a,b in list_1:
c[a].extend(b) # add to existing list or create a new one
list_2 = list(c.items())
result:
[('AAW', [147, 124]), ('AAA', [123, 456])]
note that the converted data is probably better left as dictionary. Converting to list again loses the "key" feature of the dictionary.
On the other hand, if you want to retain the order of the "keys" of the original list of tuples, unless you're using python 3.6/3.7, you'd have to create a list with the original "keys" (ordered, unique), then rebuild the list from the dictionary. Or use an OrderedDict
but then you cannot use defaultdict
(or use a recipe)
The most performant approach is to use a collections.defaultdict
dictionary to store data as an expanding list, then convert back to tuple/list if needed:
import collections
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
c = collections.defaultdict(list)
for a,b in list_1:
c[a].extend(b) # add to existing list or create a new one
list_2 = list(c.items())
result:
[('AAW', [147, 124]), ('AAA', [123, 456])]
note that the converted data is probably better left as dictionary. Converting to list again loses the "key" feature of the dictionary.
On the other hand, if you want to retain the order of the "keys" of the original list of tuples, unless you're using python 3.6/3.7, you'd have to create a list with the original "keys" (ordered, unique), then rebuild the list from the dictionary. Or use an OrderedDict
but then you cannot use defaultdict
(or use a recipe)
edited 7 hours ago
answered 8 hours ago
Jean-François Fabre
92.1k847102
92.1k847102
1
Note that this answer would not necessarily retain the order of the original list, if that is a consideration.
â blhsing
7 hours ago
true, unless you're using python 3.7. To retain the original order you'd have to create a list with the original "keys" (ordered, unique), then rebuild the list from the dictionary.
â Jean-François Fabre
7 hours ago
add a comment |Â
1
Note that this answer would not necessarily retain the order of the original list, if that is a consideration.
â blhsing
7 hours ago
true, unless you're using python 3.7. To retain the original order you'd have to create a list with the original "keys" (ordered, unique), then rebuild the list from the dictionary.
â Jean-François Fabre
7 hours ago
1
1
Note that this answer would not necessarily retain the order of the original list, if that is a consideration.
â blhsing
7 hours ago
Note that this answer would not necessarily retain the order of the original list, if that is a consideration.
â blhsing
7 hours ago
true, unless you're using python 3.7. To retain the original order you'd have to create a list with the original "keys" (ordered, unique), then rebuild the list from the dictionary.
â Jean-François Fabre
7 hours ago
true, unless you're using python 3.7. To retain the original order you'd have to create a list with the original "keys" (ordered, unique), then rebuild the list from the dictionary.
â Jean-François Fabre
7 hours ago
add a comment |Â
up vote
4
down vote
You can use a dict to keep track of the indices of each key to keep the time complexity O(n):
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
list_2 =
i =
for k, s in list_1:
if k not in i:
list_2.append((k, s))
i[k] = len(i)
else:
list_2[i[k]][1].extend(s)
list_2
would become:
[('AAA', [123, 456]), ('AAW', [147, 124])]
add a comment |Â
up vote
4
down vote
You can use a dict to keep track of the indices of each key to keep the time complexity O(n):
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
list_2 =
i =
for k, s in list_1:
if k not in i:
list_2.append((k, s))
i[k] = len(i)
else:
list_2[i[k]][1].extend(s)
list_2
would become:
[('AAA', [123, 456]), ('AAW', [147, 124])]
add a comment |Â
up vote
4
down vote
up vote
4
down vote
You can use a dict to keep track of the indices of each key to keep the time complexity O(n):
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
list_2 =
i =
for k, s in list_1:
if k not in i:
list_2.append((k, s))
i[k] = len(i)
else:
list_2[i[k]][1].extend(s)
list_2
would become:
[('AAA', [123, 456]), ('AAW', [147, 124])]
You can use a dict to keep track of the indices of each key to keep the time complexity O(n):
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
list_2 =
i =
for k, s in list_1:
if k not in i:
list_2.append((k, s))
i[k] = len(i)
else:
list_2[i[k]][1].extend(s)
list_2
would become:
[('AAA', [123, 456]), ('AAW', [147, 124])]
edited 31 mins ago
codeforester
15.9k73555
15.9k73555
answered 8 hours ago
blhsing
16.5k2631
16.5k2631
add a comment |Â
add a comment |Â
up vote
1
down vote
Similarly to other answers, you can use a dictionary to associate each key with a list of values. This is implemented in the function merge_by_keys
in the code snippet below.
import pprint
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
list_2 = [("AAA", [123, 456]), ("AAW", [147, 124])]
def merge_by_key(ts):
d =
for t in ts:
key = t[0]
values = t[1]
if key not in d:
d[key] = values[:]
else:
d[key].extend(values)
return d.items()
result = merge_by_key(list_1)
pprint.pprint(result)
Why are you manually creating list_2?
â codeforester
20 mins ago
add a comment |Â
up vote
1
down vote
Similarly to other answers, you can use a dictionary to associate each key with a list of values. This is implemented in the function merge_by_keys
in the code snippet below.
import pprint
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
list_2 = [("AAA", [123, 456]), ("AAW", [147, 124])]
def merge_by_key(ts):
d =
for t in ts:
key = t[0]
values = t[1]
if key not in d:
d[key] = values[:]
else:
d[key].extend(values)
return d.items()
result = merge_by_key(list_1)
pprint.pprint(result)
Why are you manually creating list_2?
â codeforester
20 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Similarly to other answers, you can use a dictionary to associate each key with a list of values. This is implemented in the function merge_by_keys
in the code snippet below.
import pprint
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
list_2 = [("AAA", [123, 456]), ("AAW", [147, 124])]
def merge_by_key(ts):
d =
for t in ts:
key = t[0]
values = t[1]
if key not in d:
d[key] = values[:]
else:
d[key].extend(values)
return d.items()
result = merge_by_key(list_1)
pprint.pprint(result)
Similarly to other answers, you can use a dictionary to associate each key with a list of values. This is implemented in the function merge_by_keys
in the code snippet below.
import pprint
list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
list_2 = [("AAA", [123, 456]), ("AAW", [147, 124])]
def merge_by_key(ts):
d =
for t in ts:
key = t[0]
values = t[1]
if key not in d:
d[key] = values[:]
else:
d[key].extend(values)
return d.items()
result = merge_by_key(list_1)
pprint.pprint(result)
answered 7 hours ago
Odysseas
445210
445210
Why are you manually creating list_2?
â codeforester
20 mins ago
add a comment |Â
Why are you manually creating list_2?
â codeforester
20 mins ago
Why are you manually creating list_2?
â codeforester
20 mins ago
Why are you manually creating list_2?
â codeforester
20 mins ago
add a comment |Â
up vote
1
down vote
You can create a dictionary and loop through the list. If the item present in dictionary append the value to already existing list else assign the value to key.
dict_1 =
for item in list_1:
if item[0] in dict_1:
dict_1[item[0]].append(item[1][0])
else:
dict_1[item[0]] = item[1]
list_2 = list(dict_1.items())
New contributor
It does the merge but doesn't retain the original order in the list. The result I got is:[('AAW', [147, 124]), ('AAA', [123, 456])]
- AAA should have been the first element as in the original list.
â codeforester
22 mins ago
add a comment |Â
up vote
1
down vote
You can create a dictionary and loop through the list. If the item present in dictionary append the value to already existing list else assign the value to key.
dict_1 =
for item in list_1:
if item[0] in dict_1:
dict_1[item[0]].append(item[1][0])
else:
dict_1[item[0]] = item[1]
list_2 = list(dict_1.items())
New contributor
It does the merge but doesn't retain the original order in the list. The result I got is:[('AAW', [147, 124]), ('AAA', [123, 456])]
- AAA should have been the first element as in the original list.
â codeforester
22 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can create a dictionary and loop through the list. If the item present in dictionary append the value to already existing list else assign the value to key.
dict_1 =
for item in list_1:
if item[0] in dict_1:
dict_1[item[0]].append(item[1][0])
else:
dict_1[item[0]] = item[1]
list_2 = list(dict_1.items())
New contributor
You can create a dictionary and loop through the list. If the item present in dictionary append the value to already existing list else assign the value to key.
dict_1 =
for item in list_1:
if item[0] in dict_1:
dict_1[item[0]].append(item[1][0])
else:
dict_1[item[0]] = item[1]
list_2 = list(dict_1.items())
New contributor
edited 23 mins ago
codeforester
15.9k73555
15.9k73555
New contributor
answered 8 hours ago
S.Harish
644
644
New contributor
New contributor
It does the merge but doesn't retain the original order in the list. The result I got is:[('AAW', [147, 124]), ('AAA', [123, 456])]
- AAA should have been the first element as in the original list.
â codeforester
22 mins ago
add a comment |Â
It does the merge but doesn't retain the original order in the list. The result I got is:[('AAW', [147, 124]), ('AAA', [123, 456])]
- AAA should have been the first element as in the original list.
â codeforester
22 mins ago
It does the merge but doesn't retain the original order in the list. The result I got is:
[('AAW', [147, 124]), ('AAA', [123, 456])]
- AAA should have been the first element as in the original list.â codeforester
22 mins ago
It does the merge but doesn't retain the original order in the list. The result I got is:
[('AAW', [147, 124]), ('AAA', [123, 456])]
- AAA should have been the first element as in the original list.â codeforester
22 mins ago
add a comment |Â
Theprofessor14 is a new contributor. Be nice, and check out our Code of Conduct.
Theprofessor14 is a new contributor. Be nice, and check out our Code of Conduct.
Theprofessor14 is a new contributor. Be nice, and check out our Code of Conduct.
Theprofessor14 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52454582%2fmerge-tuples-with-the-same-key%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Related: Python: merge lists of tuples based on its values
â Aran-Fey
8 hours ago
2
all those years and not a single exact dupe... I had a good look. Maybe it's somewhere, well...
â Jean-François Fabre
7 hours ago