I am having a problem with the Union of sets not giving me the correct Length.

ConnectingLists = 
Do[AConnectingList = {}; For[i = 1, i <= Length[PlayerACountries], i++,
AConnect = PlayerACountries[[i]][[4]];
AConnectingList = Union[AConnectingList, AConnect]];, {1}]

This code should return a list of unique elements from the 4th part of each element in PlayerACountries. There is a total of 14 elements in PlayerACountries.

It however returns the following:

In[81]:= AConnectingList

Out[81]= {"Brazil, WesternEurope, SouthernEurope, Egypt, EastAfrica, \
CentralAfrica", "CentralAfrica, EastAfrica, Madagascar", \
"CentralAmerica, Brazil, Peru", "China, Siberia, Japan, Irkutsk, \
Kamchatka", "Indonesia, EasternAustralia", "Mongolia, Siberia, \
Yakutsk, Kamchatka", "NorthAfrica, EastAfrica, SouthAfrica", \
"NorthwestTerritory, Alberta, Kamchatka", "Ontario, \
EasternUnitedStates, Greenland", "Peru, Brazil", "Russia, \
Afghanistan, China, Siberia", "Russia, MiddleEast, India, China, \
Ural", "SouthAfrica, EastAfrica", "WesternEurope, NorthernEurope, \
Iceland, Scandinavia"}

Which has length:

In[82]:= Length[AConnectingList]

Out[82]= 14

Why is this not just one single list of elements that should have a much higher length than 14? Also is there any way to make sure that I can do this - which means the Union should also remove the duplicate elements as well??


  • $\begingroup$ Your code is weird: Why Do only one time? Why use For? AConnectingList=DeleteDuplicates@PlayerACountries[[All,4]] should do the job in one line. $\endgroup$
    – Jinxed
    Mar 20, 2015 at 10:37
  • 1
    $\begingroup$ You have several country names seprated by commas collected in a single string for a total of 14 strings. You won't see that in Mathematica's output as it hides the quotes by default (that's settable behaviour). On SE the quotes become visible upon pasting the output here. Examine it closely. $\endgroup$ Mar 20, 2015 at 12:13
  • $\begingroup$ @Jinxed: I guess it wasn't done for that reason here (see at the end of the comment), but Do[expression,{1}] at top level is a reliable way to prevent the value of the expression not only from output, but also from being stored in Out (if you just put a semicolon at the end, it doesn't get printed, but still gets stored in Out). Just writing Do[expression] (without the {1}) also works, but has the disadvantage that the syntax highlighter tells you there is a second argument missing. However assigning the resulting Null to ConnectingList doesn't seem meaningful. $\endgroup$
    – celtschk
    Mar 20, 2015 at 14:18

1 Answer 1


I'd suggest to use



  • $\begingroup$ @Christopher: Did my suggestion work for you? $\endgroup$
    – Jinxed
    Mar 21, 2015 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.