Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a string into set() wiithout it breaking the string up?

Tags:

python

set

new_set = set('Hello')

I want just {'Hello'} instead of each individual character as a part of the set

like image 807
dustinyourface Avatar asked Dec 09 '22 03:12

dustinyourface


2 Answers

Use a set literal:

new_set = {'hello'}
like image 56
Ismail Badawi Avatar answered May 19 '23 01:05

Ismail Badawi


The set constructor expects an iterable, and it'll add each item of the iterable to the set. Strings are iterable. Hence, each letter of the string is added to the set.

That being said, do this instead:

new_set = set(['Hello'])
like image 28
IcarianComplex Avatar answered May 19 '23 03:05

IcarianComplex