Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Any Iterable to Array in Python

This just has to be a dupe, but I just didn't find any existing instance of this question...

What is the easiest way to convert any iterable to an array in Python (ideally, without importing anything)?

Note: Ideally, if the input is an array then it shouldn't duplicate it (but this isn't required).

like image 378
user541686 Avatar asked Nov 29 '22 03:11

user541686


1 Answers

It depends on what you mean by array. If you really mean array and not list or the like, then you should be aware that arrays are containers of elements of the same (basic) type (see http://docs.python.org/library/array.html), i.e. not all iterables can be converted into an array. If you mean list, try the following:

l = list(iterable)
like image 84
jena Avatar answered Dec 05 '22 13:12

jena