[Tutor] Final review

Mark Lawrence breamoreboy at yahoo.co.uk
Thu May 1 17:53:39 CEST 2014


On 01/05/2014 06:21, Scott W Dunning wrote:
> Hello, I am new to python and have a final review coming up and was hoping you could help me answer a few questions I came across while studying.
>
> So, I get a little confused about lists sometimes.  This one is a little hard to make heads or tails of.  I get confused about how to tell how many lists are within one list like the one below.   How many lists are located inside alist is it 1 or 2, 3??  Also, do spaces count as an index in lists?
>
>>>> alist = [3, 67, "cat”, [56, 57, “dog”], [], 3.14, False]
>>>> print alist[4:]
> [[], 3.14, False]
>
> The ouput for below is 2 when it seems like there should be 3 lists located inside x.  Is it [10,20] that is not consider inside of x?    Any tips on how to tell how to spot them more clearly?
>
> x = ['a', [2.0, 5, [10, 20]]]
> print len(x)
>
> I have a few more questions too but I figured I send this email for now while compile my other questions you guys can hopefully help to shed light on.
>
> Thanks for any help!!
>
> Scott

One way of showing what you've got in the list using iPython.

In [2]: alist = [3, 67, "cat", [56, 57, 'dog'], [], 3.14, False]

In [3]: for i, item in enumerate(alist):
    ...:     print('index', i, type(item), 'item', item)
    ...:
index 0 <class 'int'> item 3
index 1 <class 'int'> item 67
index 2 <class 'str'> item cat
index 3 <class 'list'> item [56, 57, 'dog']
index 4 <class 'list'> item []
index 5 <class 'float'> item 3.14
index 6 <class 'bool'> item False

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com




More information about the Tutor mailing list