1

I have four different variables, say,

s1 = 'Foo'
s2 = 'Boo'
s3 = 'Foo'
s4 = 'Duh'

Now, I would like to form a string of every unique value from all of s1, s2, s3 and s4 keeping the order. Like in the above case the string would be something like:

"We have a collection of types 'Foo', 'Boo' and 'Duh'."

Is there a simple way to achieve this?

4
  • 2
    Something like: set([s1,s2,s3,s4]) ? Commented Aug 18, 2016 at 6:43
  • If sequence doesn't matter, Mamka's solution should work well. Commented Aug 18, 2016 at 6:44
  • @YiFei How can we modify Mamka's solution if sequence does matter? Commented Aug 18, 2016 at 8:31
  • Set itself won't preserve any order, so you have to use a list. Wiktor has already posted an answer. If you have really a large number of variable, you can utilize python's built-in reflective programming and maintain both a list and set to ensure the performance, if needed. And if you want to preserve the order, you should specify it in your post, then it won't be marked as duplicate. Maybe it's still duplicate, I'm not sure :) Commented Aug 18, 2016 at 8:57

1 Answer 1

3

To get rid of duplicates and preserve the order of occurrence, you need to append values "manually":

s = "We have a collection of types {}."
lst = [s1,s2,s3,s4]
fin = []
for x in lst:
    if x not in fin:
        fin.append(x)
print(s.format(", ".join(fin)))

See this Python demo

If you were not going to preserve the order, you might use set that would return unique items in a list (just create a list from your variables):

s1 = 'Foo'
s2 = 'Boo'
s3 = 'Foo'
s4 = 'Duh'
print("We have a collection of types {}.".format(", ".join(set([s1,s2,s3,s4]))))

where:

  • "We have a collection of types {}.".format() is the string format method where the string literal contains a {} that is a placeholder for the only argument passed to the format method
  • ", ".join() - a method to make a string out of a list joining the items with a comma+space
  • set() - a method that takes an iterable and returns a frozenset, unique items in the iterable
  • [s1,s2,s3,s4] - a list created from the standalone vars

See Python demo

Sign up to request clarification or add additional context in comments.

6 Comments

This did the trick. If you don't mind, can you explain what format, join does in print("We have a collection of types {}.".format(", ".join(list(set([s1,s2,s3,s4])))))?
I updated the answer with explanations. I have double checked, and actually, you can even do without a list() as join will join the items in the set() result.
Great. How can I modify the above to put them in sequence s1,s2,s3,s4. That is, in the way that others should follow s1's value always.
Do you mean that Duh, Foo, Boo is not what you want, and you want Foo, Boo, Duh?
That's right. I want to make sure that Foo (or whatever is s1's value) comes first.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.