Home > Software design >  How do I convert a list into a set without breaking down number?
How do I convert a list into a set without breaking down number?

Time:03-04

I am trying to convert a list of times into a set so I can compare them to another set, but when I use

print(set(key))

it returns the set as {'1', '7', ':', '0', ' '}.

key originally contains this:

09:00
13:00
13:00
13:00
14:00
17:00
13:00
13:15
18:00
13:00
13:00
13:00
15:00
13:00
13:00
13:00
14:00
13:00
13:00
17:00

Is there a way to return the set so that it just contains {'17:00'} and is not broken down?

CodePudding user response:

try the following code

set(key.split())

CodePudding user response:

if you have key values in list then print(set(key)) gives expected output. like

key = ["9:00","13:00","13:00","15:00"]
print(set(key))

if key is string with whitespace seperating the time values then

key = "9:00 13:00 13:00 15:00"
key_split = key.split(" ") #splits with spaces key.split() also works same as key.split(" ")
print(set(key_split))
  • Related