Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Best way to get first single value of list

What is the fastest and best way to get first value from list?
There are multiple techniques, but what is the best (memory and speed) and why?

Examples below. I would be grateful for other techniques as well:

listis = [1, 2, 3, 4]

### 1:
###----------

value = next(iter(listis))

print(value)

### 2:
###----------

value = listis[0]

print(value)

### 3:
###----------

value, *args = listis

print(value)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Calling the list element by its element number is always the fastest way. You aren’t doing any calculation nor using any functions to get to it.

print(listis[0])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading