position: fixed; top: auto !important; margin-left: 112px;

Python Lists

Python lists are similar to arrays declared in other languages. But the most powerful thing is that list need not be always homogeneous. A single list can contain strings, integers, as well as objects. The list is a collection which is ordered and changeable.

The basic example of List

# Create a list ls = [5, 7, 25] # Prints the type of the variable print(type(ls)) # Print the contents of the list print(ls) # elementwise indexing in the list print(ls[0], ls[1], ls[2]) # Append an element at the end of the list ls.append(45) print(ls) # Remove and return the last element of the list x = ls.pop() print(x, ls)

Example 2

# Unlike arrays, list can contain elements of different dataypes ls = ['codzify', ".com", 23] # Print the contents of the list print(ls) # indexing in list print(ls[0], ls[1], ls[2]) # Append an element at the end of the list ls.append('hello world') print(ls) # Remove and return the last element of the list x = ls.pop() print(x, ls)

Slicing in Python

Other than accessing list elements one at a time, we can access the sublists python slice. Comments in the given example will help you to understand the concepts properly. Try to execute the code to see the output.

ls = list(range(6)) print(ls) # Negative Indexing is also possible, it will print from the end of the lis print(ls[-1], ls[-2], ls[-3]) # Get a sublist starting from index 1 to 3 (last index is exclusive) print(ls[1:4]) # Get a sublist starting from index 2 to the end print(ls[2:]) # Get a sublist from start to index 3 (last index is exclusive) print(ls[:4]) # Get the whole list print(ls[:]) # Assign a new sublist to a slice ls[2:4] = ['codzify', 'python'] print(ls)

Looping through the list

This example ilustrates you how to loop through the elements of list in python.

languages = ['C', 'C++', 'Python', 'Java', 'Swift'] for x in languages: # Some Code print(x)

List Comprehensions

Python list Comprehensions are used whenever you want to frequently transform one type of data into another.

To understand the concept we will demonstrate to you using the old traditional method to write programs and what you can do using python list comprehension.

Example 1:

Using an old traditional way find the square of numbers in python.

# Traditional way nums = list(range(1,5)) squares = [] for x in nums: squares.append(x ** 2) print(squares)

Using List Comprehension find the square of numbers in python.

# creates a list from range 1 to 5 nums = list(range(1,5)) squares = [x ** 2 for x in nums] print(squares)

Example 2:

Find the even and odd numbers from 1 to 10 using old traditional way

# creates a list from range 1 to 10 nums = list(range(10)) even = [] odd = [] for i in nums: if i%2 == 0: even.append(i) else: odd.append(i) print('Even Numbers: ', even) print('Odd Numbers : ', odd)

Find the even and odd numbers from 1 to 10 using List Comprehension

nums = list(range(10)) even = [x for x in nums if x%2 == 0] odd = [x for x in nums if x%2 != 0] print('Even Numbers: ', even) print('Odd Numbers : ', odd)

Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.