how to extract but keep together items from list within list nested within another list

  • Last Update :
  • Techknowledgy :

Use list comprehension,

>>> new_list = [
      [y[1]
         for y in x
      ]
      for x in my_list
   ] >>>
   new_list[[2, 4], [6, 8], [10, 12]]

You don't have to use a list comprehension (although it is neater), so here is a working example just using for loops. You were very close, you just needed to append your items into lists which you then appended into a big list! :-)

my_list = [
   [
      [1, 2],
      [3, 4]
   ],
   [
      [5, 6],
      [7, 8]
   ],
   [
      [9, 10],
      [11, 12]
   ]
]

result = []
for sub_list in my_list:
   result_sublist = []
for item in sub_list:
   result_sublist.append(item[1])
result.append(result_sublist)

print(result)

Suggestion : 2

The negative indexes for the items in a nested list are illustrated as below:,A list can contain any sort object, even another list (sublist), which in turn can contain sublists themselves, and so on. This is known as nested list.,A nested list is created by placing a comma-separated sequence of sublists.,You can access individual items in a nested list using multiple indexes.

L = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', 'h']
L = ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']

print(L[2])
# Prints['cc', 'dd', ['eee', 'fff']]

print(L[2][2])
# Prints['eee', 'fff']

print(L[2][2][0])
# Prints eee
L = ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']

print(L[-3])
# Prints['cc', 'dd', ['eee', 'fff']]

print(L[-3][-1])
# Prints['eee', 'fff']

print(L[-3][-1][-2])
# Prints eee
L = ['a', ['bb', 'cc'], 'd']
L[1][1] = 0
print(L)
# Prints['a', ['bb', 0], 'd']
L = ['a', ['bb', 'cc'], 'd']
L[1].append('xx')
print(L)
# Prints['a', ['bb', 'cc', 'xx'], 'd']
L = ['a', ['bb', 'cc'], 'd']
L[1].insert(0, 'xx')
print(L)
# Prints['a', ['xx', 'bb', 'cc'], 'd']

Suggestion : 3

Last Updated : 10 Jul, 2022

1._
Input: [
   [1, 2],
   [3, 4, 5],
   [6, 7, 8, 9]
]
Output: [1, 3, 6]

Input: [
   ['x', 'y', 'z'],
   ['m'],
   ['a', 'b'],
   ['u', 'v']
]
Output: ['x', 'm', 'a', 'u']

[1, 3, 6]

[1, 3, 6]

Suggestion : 4

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.,Definition: A list of lists in Python is a list object where each list element is a list by itself. Create a list of list in Python by using the square bracket notation to create a nested list [[1, 2, 3], [4, 5, 6], [7, 8, 9]].,Solution: Use two nested list comprehension statements, one to create the outer list of lists, and one to create the inner lists.,Use the list comprehension statement [tuple(x) for x in list] to convert each element in your list to a tuple. This works also for list of lists with varying number of elements.

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 : 5

Vectors are basic objects in R and they can be subsetted using the [ operator.,The [[ operator is used to extract elements of a list or a data frame. It can only be used to extract a single element and the class of the returned object will not necessarily be a list or data frame.,The $ operator is used to extract elements of a list or data frame by literal name. Its semantics are similar to that of [[.,The [ operator can be used to extract multiple elements of a vector by passing the operator an integer sequence. Here we extract the first four elements of the vector.

> x < -c("a", "b", "c", "c", "d", "a") >
   x[1] # # Extract the first element[1]
"a" >
x[2] # # Extract the second element[1]
"b"
> x[1: 4]
   [1]
"a"
"b"
"c"
"c"
> x[c(1, 3, 4)]
   [1]
"a"
"c"
"c"
> u < -x > "a" >
   u[1] FALSE TRUE TRUE TRUE TRUE FALSE >
   x[u]
   [1]
"b"
"c"
"c"
"d"
> x[x > "a"]
   [1]
"b"
"c"
"c"
"d"
> x < -matrix(1: 6, 2, 3) >
   x[, 1][, 2][, 3]
   [1, ] 1 3 5[2, ] 2 4 6