Python List

0 0
Read Time:2 Minute, 57 Second

Python List

A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.

The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1.
The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator.

Example:

x= [ 'abcd', 11, 2.23, 'Ram', 70.2 ]
thislist = [123, 'Ram']

print(x)          # Prints complete list
print(x[0])       # Prints first element of the list
print(x[1:3])    # Prints elements starting from 2nd till 3rd 
print(x[2:])     # Prints elements starting from 3rd element
print(thislist * 2)  # Prints list two times
print(x+ thislist) # Prints concatenated lists

Output:

['abcd', 11, 2.23, 'Ram', 70.2]
abcd
[11, 2.23]
[2.23, 'Ram', 70.2]
[123, 'Ram', 123, 'Ram']
['abcd', 11, 2.23, 'Ram', 70.2, 123, 'Ram']

Ordered

It means that the items have a defined order, and that order will not change.

Changeable

We can change, add, and remove items in a list after it has been created.

Allow Duplicates

lists can have items with the same value.

List Length

Use len() function to get how many items in list.

thislist = ["apple", "banana", "cherry"]
print(len(thislist))
#Output:3

List type

Use type() function to get the type of any python object.

mylist = ["apple", "banana", "cherry"]
print(type(mylist))
#Output:<class 'list'>

The list() Constructor

Using list() constructor to create a new list.

thislist = list(("apple", "banana", "cherry"))

List Operations:

  • Access List Items

List items are indexed and you can access them by referring to the index number:
The first item has index 0.

fruits= ["apple", "banana", "cherry"]
print(fruits[1])

Negative Indexing

Negative indexing means start from the end.
-1 refers to the last item, -2 refers to the second last item etc.

Print the last item of the list:

fruits = ["apple", "banana", "cherry"]
print(fruits[-1])

Range of Indexes

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.

Example

Return the third, fourth, and fifth item:

fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(fruits[2:5])

The search will start at index 2 (included) and end at index 5 (not included).

Example

By leaving out the start value, the range will start at the first item:

fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(fruits[:4])

Example

By leaving out the end value, the range will go on to the end of the list:

fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(fruits[2:])

Check if Item Exists

fruits = ["apple", "banana", "cherry"]
print("apple" in fruits)
#Output: True
  • Change List Items

Example: Change Item Value

To change the value of a specific item, refer to the index number:

fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango"

Example: Change a Range of Item Values

To change the value of items within a specific range, define a list with the new values, and refer to the range of index numbers where you want to insert the new values:

fruits = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
fruits[1:3] = ["blackcurrant", "watermelon"]
print(fruits)
#Output : ['apple', 'blackcurrant', 'watermelon', 'orange', 'kiwi', 'mango']

  • Add List Items
  • Remove List Items
  • Loop Lists
  • List Comprehension
  • Sort Lists
  • Copy Lists
  • List Methods
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment