It's not possible to use string indices with a list
in python. Lists have numeric indices starting from 0
up to len(my_list)-1
.
If you were to use the list()
call itself, it requires an iterable
variable:
>>> help(list)
class list(object)
| list() -> new empty list
| list(iterable) -> new list initialized from iterable's items
So you could construct a tuple and pass that to the list()
class like:
>>> my_list = list((df1, df2, df3))
>>> type(my_list)
<class 'list'>
>>> my_list[0]
... df1 outputs here ...
But a simpler, and cleaner, way to do it is using the square brackets notation:
>>> my_list = [df1, df2, df3]
>>> type(all_dataframes)
<class 'list'>
However, if you want to use string indices, then think about using a dictionary i.e. the dict
class:
>>> help(dict)
class dict(object)
| dict() -> new empty dictionary
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs
| dict(iterable) -> new dictionary initialized as if via:
| d = {}
| for k, v in iterable:
| d[k] = v
| dict(**kwargs) -> new dictionary initialized with the name=value pairs
| in the keyword argument list. For example: dict(one=1, two=2)
|
| Methods defined here:
|
Calling the dict()
class directly, you'd want something like this:
>>> all_dataframes = dict(("df1", df1), ("df2", df2), ("df3", df3))
>>> type(all_dataframes)
<class 'dict'>
>>> all_dataframes["df1"]
... df1 output prints here ...
But, the simpler and clearer method would be:
>>> all_dataframes = {"df1": df1, "df2": df2, "df3": df3}
>>> type(all_dataframes)
<class 'dict'>