How Would You Make a Comma-Separated String from a List of Strings

How would you make a comma-separated string from a list of strings?

my_list = ['a', 'b', 'c', 'd']
my_string = ','.join(my_list)
'a,b,c,d'

This won't work if the list contains integers


And if the list contains non-string types (such as integers, floats, bools, None) then do:

my_string = ','.join(map(str, my_list)) 

How create a comma-separated string from items in a list

This is how I do it using join() in List:

main(List<String> arguments) {
List cities = ['NY', 'LA', 'Tokyo'];

String s = cities.join(', ');
print(s);
}

Convert a list of string into a comma separated string without duplications

The best to avoid duplicates is to use a Set

  • TreeSet for alphabetical order

  • LinkedHashSet to keep insertion order (initial order)

StringBuilder result = new StringBuilder();
String delimiter= "";
for (String i : new LinkedHashSet<String>(list)) {
result.append(delimiter).append(i);
delimiter = ",";
}
return result.toString();

But you can just do String.join(',', new LinkedHashSet<String>(list))


Back to List

List<String> inputList = (unique) ? new ArrayList<>(new HashSet<>(list)) : list;

Case-insensitive

Set<String> check = new HashSet<String>();
StringBuilder result = new StringBuilder();
String delimiter= "";
for (String i : list) {
if(!check.contains(i.toLowerCase())){
result.append(delimiter).append(i);
delimiter = ",";
check.add(i.toLowerCase());
}
}
return result.toString();

How can I process a list of strings where each string may be a comma-separated list of strings as well?

You can split on",", then flatten the sub lists with itertools.chain.from_iterable:

>>> from itertools import chain
>>> lst = ['orange','cherry,strawberry','cucumber,tomato,coconut,avocado','apple','blueberry,banana']
>>> list(chain.from_iterable(x.split(",") for x in lst))
['orange', 'cherry', 'strawberry', 'cucumber', 'tomato', 'coconut', 'avocado', 'apple', 'blueberry', 'banana']

Creating comma-separated string from list

str.join only accepts an iterable of strings, not one of integers. From the docs:

str.join(iterable)

Return a string which is the concatenation of the strings in the iterable iterable.

Thus, you need to convert the items in x into strings. You can use either map or a list comprehension:

x = [3, 1, 4, 1, 5]
y = ",".join(map(str, x))

x = [3, 1, 4, 1, 5]
y = ",".join([str(item) for item in x])

See a demonstration below:

>>> x = [3, 1, 4, 1, 5]
>>>
>>> ",".join(map(str, x))
'3,1,4,1,5'
>>>
>>> ",".join([str(item) for item in x])
'3,1,4,1,5'
>>>

How to split a comma-separated string into a list of strings?

You have to split the input using input().split(',').

a1=input().split(',')
print(a1)

Convert a list into a comma-separated string

Enjoy!

Console.WriteLine(String.Join(",", new List<uint> { 1, 2, 3, 4, 5 }));

First Parameter: ","
Second Parameter: new List<uint> { 1, 2, 3, 4, 5 })

String.Join will take a list as a the second parameter and join all of the elements using the string passed as the first parameter into one single string.

How convert a list of strings to a single delimited string?

There is a problem with getting items from json. Here is the way to get all items in image_list correctly:

data = {'result':
{'tags': [
{'confidence': 35.0027923583984,
'tag': {'en': 'machinist'}},
{'confidence': 30.3697471618652,
'tag': {'en': 'seller'}},
{'confidence': 28.2139053344727,
'tag': {'en': 'man'}},
{'confidence': 27.542501449585,
'tag': {'en': 'work'}},
{'confidence': 23.9936943054199,
'tag': {'en': 'worker'}},
{'confidence': 23.8494567871094,
'tag': {'en': 'working'}},
{'confidence': 23.012264251709,
'tag': {'en': 'person'}}
]
}
}

image_list = []
for tags in data['result']['tags']:
# Check whether confidence is > 20 or not
# if so add it to list, else do nothing
if tags['confidence'] > 20:
image_list.append(tags['tag']['en'])

# image_list -> ['machinist', 'seller', 'man', ...]

result = ",".join(image_list)
# -> 'machinist,seller,man,.."

Convert a list of integers into a comma-separated string

Yes. However, there is no Collectors.joining for a Stream<Integer>; you need a Stream<String> so you should map before collecting. Something like,

System.out.println(i.stream().map(String::valueOf)
.collect(Collectors.joining(",")));

Which outputs

1,2,3,4,5

Also, you could generate Stream<Integer> in a number of ways.

System.out.println(
IntStream.range(1, 6).boxed().map(String::valueOf)
.collect(Collectors.joining(","))
);


Related Topics



Leave a reply



Submit