4

I don't understand how the list function works.

Here is the research I have done:

Documentation I am looking at:

documentation

In particular, I am looking at this paragraph:

class list([iterable]) Return a list whose items are the same and in the same order as iterable’s items. iterable may be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a list, a copy is made and returned, similar to iterable[:]. For instance, list('abc') returns ['a', 'b', 'c'] and list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, returns a new empty list, [].

list is a mutable sequence type, as documented in Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange. For other containers see the built in dict, set, and tuple classes, and the collections module.

Here is another post:

Another post about the list function

On that post, the someone posts the following:

>>> myList = ['hello']
>>> myList=list(myList[0])
>>> myList
['h', 'e', 'l', 'l', 'o']

But when I do this:

for root, dirs, files in os.walk(os.getcwd()):
     path_files.append(files)

path_files
[['combinedPdfs.py', 'meetingminutes.pdf', 'meetingminutes_encrypted.pdf', 'pdf_intro.py', 'pdf_paranoia.py', 'readDocx.py']]

>>> path_files_2 = list(path_files[0])
>>> path_files_2
['combinedPdfs.py', 'meetingminutes.pdf', 'meetingminutes_encrypted.pdf', 'pdf_intro.py', 'pdf_paranoia.py', 'readDocx.py']
>>> path_files_2[0]
'combinedPdfs.py'
>>> path_files_2[1]
'meetingminutes.pdf'

Why did what I do work differently than the user from the other post?

Edit #1:

If I run something like this:

>>> myList2 = ['hello', 'goodbye']
>>> myList2[0]
'hello'
>>> myList2 = list(myList2)
>>> myList2
['hello', 'goodbye']
>>> myList2  = list(myList2[0])
>>> myList2
['h', 'e', 'l', 'l', 'o']

If I run something like this:

>>> myList4 = [['Hello', 'goodbye']]
>>> myList4 = list(myList4)
>>> myList4
[['Hello', 'goodbye']]
>>> myList4 = list(myList4[0])
>>> myList4
['Hello', 'goodbye']

I see the definition, but I wish there was a more "laymans" way to explain it.

  • 4
    Notice that in your case, the first (and single) element of `path_files` is another list (see the `[[...]]`). – miradulo May 12 '18 at 19:23

1 Answers1

2

With your list called path_files, this list is actually a list within a list. You can see this because of the double square brackets on the end of your list, e.g. [['combinedPdfs.py', ..., 'readDocx.py']]. A single list would have only one set of square brackets.

The command >>> path_files = list(path_files)[0], essentially, returns the first item in the list broken out as a list. So, in the case of the ['hello'] list, it broke out hello into separate characters. With your list, it broke out the first item in the list (in this case, another list) and broke it out into its own list.

While I cannot see where you defined path_files_2 (I am assuming it is the same as the new path_files), when you type path_files_2[0] you are saying to return the first item in the list (zero-indexed).

I generally don't think of it as "the list function" but rather "the list datatype" that has its own methods. When you type list(...), I don't think of it as a "function" but rather as a method of data type casting one datatype into a list datatype.