Menu

gitpiper

cast_list python Code Snippet in 2024

listintermediate

Last Updated: 16 December 2024

Casts the provided value as a list if it's not one.

  • Use isinstance() to check if the given value is enumerable.
  • Return it by using list() or encapsulated in a list accordingly.
def cast_list(val): return list(val) if isinstance(val, (tuple, list, set, dict)) else [val]
cast_list('foo') # ['foo'] cast_list([1]) # [1] cast_list(('foo', 'bar')) # ['foo', 'bar']

python snippet similar to cast_list For You in December 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️