create a list of lists. modify elements in the list

  • Last Update :
  • Techknowledgy :

Let’s start from basic, quickest way to create and initialize a normal list with same values in python is,

# Creating a list with same values
list_of_num = [5] * 10

print(list_of_num)

[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

Let’s inserting an element in the 3rd sub list of main list i.e.

# Insert 11 into the 3 rd sub list
list_of_num[2].append(11)

Let’s insert the element in the 3rd sub list,

# Insert 11 into the 3 rd sub list
list_of_lists[2].append(11)

print(list_of_lists)

Using Python’s range() function, we can generate a sequence of numbers from 0 to n-1 and for each element in the sequence create & append a sub-list to the main list using List Comprehension i.e.

# Create a list of 5 empty sub lists
list_of_lists = [
   []
   for i in range(5)
]

print('List of lists:')
print(list_of_lists)

Suggestion : 2

Problem: How to create a list of lists by modifying each element of an original list of lists?,Problem: Given a list of lists. How to flatten the list of lists by getting rid of the inner lists—and keeping their elements?,Problem: How to print a list of lists with a new line after each list so that the columns are aligned?,Problem: How to convert a list of lists into a list of tuples?

For example, to create a list of lists of integer values, use [[1, 2], [3, 4]]. Each list element of the outer list is a nested list itself.

lst = [
   [1, 2],
   [3, 4]
]

Find examples of all three methods in the following code snippet:

lst = [
   [1, 2],
   [3, 4]
]

# Method 1: List Comprehension
flat_1 = [x
   for l in lst
   for x in l
]

# Method 2: Unpacking
flat_2 = [ * lst[0], * lst[1]]

# Method 3: Extend Method
flat_3 = []
for l in lst:
   flat_3.extend(l)

# # Check results:
   print(flat_1)
#[1, 2, 3, 4]

print(flat_2)
#[1, 2, 3, 4]

print(flat_3)
#[1, 2, 3, 4]

Solution: You can achieve this by using the beautiful (but, surprisingly, little-known) feature of dictionary comprehension in Python.

persons = [
   ['Alice', 25, 'blonde'],
   ['Bob', 33, 'black'],
   ['Ann', 18, 'purple']
]

persons_dict = {
   x[0]: x[1: ]
   for x in persons
}
print(persons_dict)
# {
   'Alice': [25, 'blonde'],
   # 'Bob': [33, 'black'],
   # 'Ann': [18, 'purple']
}

Here’s the alternative code:

persons = [
   ['Alice', 25, 'blonde'],
   ['Bob', 33, 'black'],
   ['Ann', 18, 'purple']
]

persons_dict = {}
for x in persons:
   persons_dict[x[0]] = x[1: ]

print(persons_dict)
# {
   'Alice': [25, 'blonde'],
   # 'Bob': [33, 'black'],
   # 'Ann': [18, 'purple']
}

Example: Convert the following list of lists

[
   [1, 2, 3],
   [4, 5, 6]
]

Suggestion : 3

Remote Work 2022 , Remote Work 2022

Python provides an option of creating a list within a list. If put simply, it is a nested list but with one or more lists inside as an element.

Here is an example of a list of lists to make things more clear:

[
   [a, b],
   [c, d],
   [e, f]
]

Input:

# Create 2 independent lists
list_1 = [a, b, c]
list_2 = [d, e, f]

# Create an empty list
list = []

# Create List of lists
list.append(list_1)
list.append(list_2)
print(list)

Output:

[
   [a, b, c],
   [d, e, f]
]

Suggestion : 4

Last Updated : 01 Aug, 2022

Output:

["Geeks", "for", "Geeks"]

Blank List: []

List of numbers: [10, 20, 14]

List Items:
   Geeks
Geeks

Blank List: []

List of numbers: [10, 20, 14]

List Items:
   Geeks
Geeks

List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5]

List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']

Accessing a element from the list
Geeks
Geeks

Accessing a element from a Multi - Dimensional list
For
Geeks