Python Language List: Difference between revisions
Jump to navigation
Jump to search
Line 4: | Line 4: | ||
=Overview= | =Overview= | ||
A list is a mutable [[Python_Language#Sequence_Types|sequence type]] that contains zero or more elements and whose elements can be of different types. | A list is a mutable [[Python_Language#Sequence_Types|sequence type]] that contains zero or more elements and whose elements can be of different types. | ||
=List <tt>type()</tt>= | |||
The function <code>type()</code> applied to a list returns: | |||
<font size=-1> | |||
<class 'list'> | |||
</font> | |||
To check whether an instance is a list: | |||
<syntaxhighlight lang='py'> | |||
i = ... | |||
if type(i) is list: | |||
... | |||
</syntaxhighlight > | |||
For <code>list</code> subclasses: | |||
<syntaxhighlight lang='py'> | |||
i = ... | |||
if isinstance(i, list): | |||
... | |||
</syntaxhighlight > | |||
=Access to a List= | =Access to a List= | ||
==Size of a List== | ==Size of a List== |
Revision as of 01:29, 3 March 2022
Internal
Overview
A list is a mutable sequence type that contains zero or more elements and whose elements can be of different types.
List type()
The function type()
applied to a list returns:
<class 'list'>
To check whether an instance is a list:
i = ...
if type(i) is list:
...
For list
subclasses:
i = ...
if isinstance(i, list):
...
Access to a List
Size of a List
The number of elements is given by the len()
function:
l = [...]
print(len(l))
Modify a List
Modify Individual Elements
Append an Element
l.append(e)
Delete the Last Element
del l[-1]
Assign the sublist to l
:
l = l[:-1]
Delete All Elements
List Processing
Join the List Elements in a String
Join the elements of the given list in a string, using '-' as separator:
li = ['a', 'b']
s = '-'.join(li)