Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.7, How can I get/check the size of a list? [duplicate]

I've got an array (list) that I want to check if it's size is equal to 1, if it is then it needs to append a new like as shown.

## If appended data = 1 then append the new line:
if appended_data == 1:
    appeneded_data.append("") ## Add a new line if appended data has a size of 1

Should be a fairly simple thing but I can't work it out :S

Any ideas?

like image 500
Ryflex Avatar asked Jun 19 '13 21:06

Ryflex


People also ask

How do you find the number of repeated values in a list in Python?

Operator. countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. It returns the Count of a number of occurrences of value.

How do I check the size of a Python list?

Python has got in-built method – len() to find the size of the list i.e. the length of the list. The len() method accepts an iterable as an argument and it counts and returns the number of elements present in the list.

How do I find the size of a list?

There is a built-in function called len() for getting the total number of items in a list, tuple, arrays, dictionary, etc. The len() method takes an argument where you may provide a list and it returns the length of the given list.


1 Answers

Use the len() function on it:

if len(appended_data) == 1:

Short demo:

>>> len([])
0
>>> len([1])
1
>>> len([1, 2])
2
like image 189
Martijn Pieters Avatar answered Sep 18 '22 06:09

Martijn Pieters