Tech Study

Python List (With Examples)

A Python List is used to Store various datatypes in a sequence. A list is a grouping of values or items of various types. Because Python lists are mutable types, we can change their elements after they have been created. The list’s items are separated with commas (,) and encased in square brackets [].

One of the four built-in data types in Python for storing data collections is the list; the other three are the tuple, set, and dictionary, each of which has a unique purpose.

Python lists are the same as arrays defined in other languages, such as Java’s  ArrayList and C++’s vector. Using the symbol [] and commas to separate each item, a list is a collection of items.

Creating a List in Python:

In Python, a list is made by putting elements between [ ]  separated by commas.

Example:

List1 = ["apple", "mango", "cherry"]

List2 = ["horse", "elephant", "tiger"]

print(list1)

print(list2)

Output:

[‘apple’, ‘mango’, ‘cherry’]

[‘horse’, ‘elephant’, ‘tiger’]

The complexity of Creating the list

Time Complexity: O(1)

Space Complexity: O(n)

In the above example, we created a list of fruit and animals and print it.

Items in List: 

  • Items in list can be accessed by the index
  • List can store multiple type of items
  • List items allows duplicate values
  • List items are changeable

Multiple values in a List:

A list having the elements of different values

Example

list = [5, 6, 'Tech', 4, 'For', 6, 'Study']

print("\n printing the list having different  values")

print(list)

Output:

printing the list having different  values

[5, 6, ‘Tech’, 4, ‘For’, 6, ‘Study’

Ordered List: Lists are called ordered if the items are arranged in a specific order that will not change.

Changeable: We can change the list  by adding and removing the items after the creation of a list

Duplicates: The list can have multiple items  , some multiple items having the dplicate value are allowed in python.

Example

trending = ["tech", "study", "has", "the", "best", "content", "related","to", "tech"]

print(trending)

print(type(trending))   // Using type() we will know about the type of the list

Output:

[‘tech’, ‘study’, ‘has’, ‘the’, ‘best’, ‘content’, ‘related’, ‘to’, ‘tech’]

<class ‘list’>

List Operations

  • Repetition
  • Length
  • Concatenation
  • Iteration

Repetition: The list of elements can be repeated multiple times

Example:

list1 = [5, 6, 7, 8, 9]  

repeat = list1 * 2  

print(repeat)

Output:

[5, 6, 7, 8, 9, 5, 6, 7, 8, 9]

Length: It will print the length of the list

Example:

list = [5, 6, 'Tech', 4, 'For', 6, 'Study']  

print(len(list) )

Output:

7

Concatenation:

It will concatenate the two or more list in a single list.

list1 = [5, 6, 7, 8, 9]   

list2 = [5, 6, 'Tech', 4, 'For', 6, 'Study']  

# concatenation operator +  

listnew = list1 + list2  

print(listnew)

Output:

[5, 6, 7, 8, 9, 5, 6, ‘Tech’, 4, ‘For’, 6, ‘Study’]

Iteration:

Iterating a list through the for-in loop.

Example:

list = ["tech", "study", "has", "the", "best", "content", "related","to", "tech"]

for i in list:     

print(i)

Output:

tech

study

has

the

best

content

related

to

tech

Just like C++ Vector, java collections, python has collections(Arrays)

 Collection data types:

  • List
  • Tuple
  • Set
  • Dictionary

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more