Python List -Built-in Functions and Methods for Beginner

Here we talk about Basic concepts of python list, Python list-built-in list functions, and list-Built-in methods with examples

Samanthika Rajapaksa
DataDrivenInvestor

--

Photo by Hitesh Choudhary on Unsplash

In Python programming, a list is created by placing all the items (elements) inside square brackets [], separated by commas. It can have any number of items and they may be of different types (integer, float, string, etc.).

How to create a python list?

To create python list of items, you need to mention the items, separated by commas, in square brackets.

Important: you don’t need to declare the data type, because Python is dynamically-typed.

Let’s see how to create a list and print it

How to access an element from a list?

List items are indexed and you can access them by referring to the index number.

Important: The first item has an index of 0. That means 0 refers to the first item, 1 refers to the second item and etc.

Negative indexing: It starts from the end.-1 refers to the last item, -2 refers to the second-last item, etc.

Range of indexes:

You can specify a range of indexes by specifying where to start and where to end the range. When specifying a range, the return value will be a new list with the specified items.

Important: The search will start at index 2 (included) and end at index 4(not included).

Check if an item exists in the list

How to print list items using for loop

Change item value in the list

To change the value of a specific item, refer to the index number. To insert more than one item, create a list with the new values, and specify the index number where you want the new values to be inserted.

Can create an empty array?

Yes, can

How to select a random item in an array?

How to shuffle items in an array?

How can we delete a python list?

Use the del keyword.

Deleting a single element or a few elements in the list

Deleting elements using remove(), pop() and clear() methods

remove(item): Removes specified item from list.
pop(index): Removes the element from the given index.
pop(): Removes the last element.
clear(): Removes all the elements from the list.

Let's talk about Built-in list functions

  • cmp(list1,list2)- Compares elements of both lists
  • len(list)- Gives the total length of the list.
  • max(list)- Returns item from the list with max value.
  • min(list)- Returns item from the list with min value.
  • list(seq)- Converts a tuple into a list.
  • sum(list)- Returns the sum of all the elements in the list

Build-in methods in python lists

  • append() — adds an item to the end of the list
  • insert() — insert an item at a specified position
  • index() — returns the first matching index of the item specified
  • count() — returns the count of the item specified
  • sort() — sorts the list in an ascending order
  • reverse() — reverses the order of elements in the python list

Now It’s time to check your knowledge.

Try to build a password generator project. The user gives inputs as to how many symbols, numbers, and letters should include creating a password. you should randomly select numbers, symbols, and letters and want to shuffle them. Then print the password as a string

Solution:

Let us meet with python functions in the next article. Stay safe.

Gain Access to Expert View — Subscribe to DDI Intel

--

--