The difference of python list - python

What is the difference of this two list in python:
list1 = [1,2,3,4,5,6,7,8,9]
list2 = [[[[1,2,3,4,5,6,7,8,9]]]]
When I use type(list1) and type(list2), all come with list , but when I try to make some deal such as:
Using list1:
new_total=[]
for i in range(0,len(list1),3):
b=list1[i:i+3]
print(len(b))
output:
9
6
3
Using list2:
for i in range(0,len(list2),3):
b=list2[i:i+3]
print(len(b))
output:
1

Well the elements within list 2 are the first element of the list within a list within a list.
So they are both of type list, however in the first you are printing the length of three indexed values hence 3.
In the second for loop you are printing the length of the inner list within a list, that only has one element in it (another list, which contains a list that contains the list of numbers within that)
Basically you have embedded the list of numbers 4 fold as the first element
within the original list

replying for only to clarify these reponses , just to help you to understand (as a friend) , i'll give some exemples, that may help you:
list1 = [1,2,3,4,5,6,7,8,9]
list2 = [[[[1,2,3,4,5,6,7,8,9]]]]
print (list2[0][0][0][0])
print (list2[0][0][0])
print (list2[0][0])
print (list2[0])
print (list2)
Output:
1
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[[1, 2, 3, 4, 5, 6, 7, 8, 9]]
[[[1, 2, 3, 4, 5, 6, 7, 8, 9]]]
[[[[1, 2, 3, 4, 5, 6, 7, 8, 9]]]]
I hope that's clear. Good luck!

list1 = [1,2,3,4,5,6,7,8,9]
list2 = [[[[1,2,3,4,5,6,7,8,9]]]]
list1 is a list.
list2 is a list of list of list of list.
len(list1) #will get you 9.
len(list2) #will get you 1.
You will either have to iterate into the final list inside list2 or somehow flatten it into a one dimensional list.
Hope this helps point you in the right direction without directly giving out the answer.

Related

How can I put the various elements come from for loop in a list?

['aba']['b']['c']
These above element came while iterating in a loop and I want to make a list which contains these elements. How can I achieve my goal?
Make an empty list and then append results to that list. For example:
loop = [1, 2, 3, 4, 5, 6]
endresult = []
for i in loop:
if i % 2 == 0:
endresult.append(i)
Output:
[2, 4, 6]

Why does removing items from list while iterating over it skip every other item [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 3 years ago.
When iterating over a list and removing each item, why is every other item skipped and not removed? (I realise it may be bad practise to do this I just want to understand what is going on)
lst=[0,1,2,3,4,5,6,7,8,9]
for item in lst:
lst.remove(item)
print(lst)
Expected output: []
Actual output: [1, 3, 5, 7, 9]
What happens here is the following:
You start with the following list:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Then, in the first iteration of the for loop, you get the first item of the list which is 0, and remove it from the list. The updated list now looks like:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Then, at the second iteration of the loop, you get the second item in the updated list. Here, that second item is not 1 anymore but it is 2 (because you look at the updated list), so you remove 2 from the list. The updated list now looks like:
[1, 3, 4, 5, 6, 7, 8, 9]
And it goes on... until you get:
[1, 3, 5, 7, 9]
To get the expected output, you need to iterate over a shallow copy of the list, while still removing items from the original list, as follows
lst=[0,1,2,3,4,5,6,7,8,9]
for item in lst[:]: # note the `[:]`
lst.remove(item)
print(lst)
# returns []
otherwise, you are iterating over a list that changes simultaneously, cf #Tibbles's explanation.

Using a List of Indexes to Grab Values in Another List

I have two lists, one containing a set of index points that I would like to pull from a second list.
index_values = [3, 3, 6, 7]
A list of ten numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I'm trying to get the result to print the numbers at the 3, 3, 6, and 7 indexes in the second list.
Any help is appreciated.
Thank you!
You have two alternatives the way I see it.
String join with list comprehension
s = ', '.join([str(numbers[idx]) for idx in index_values]) # or '\n' for newlines
print(s)
Loop
for idx in index_values:
print(numbers[idx])

remove a list of lists while iterating over it

I know that you are not supposed to remove an element of a list while iterating over it but I have to.
I'm trying to iterate over a list of lists and if I find a value in a list of my lists i need to remove it.
This is what I've tried so far.
dict[["A1","A2"],
["B1","B2"],
["C1","C2"]]
for i in range(len(dict)):
if dict[i][0]=="A1":
dict.pop(i)
But it's giving me an error of out of range.
How can I do it with list comprehensions or any other approach?
Do you mean this?
old = [["A1","A2"], ["B1","B2"], ["C1","C2"]]
new = [x for x in old if x[0] != "A1"]
You can't. You will get an exception. Create a new list as a copy.
>>> disallowed = [1, 2, 3]
>>> my_list = [ [1, 2, 3, 4, 5, 6, 7], [3, 3, 4, 5, 8, 8, 2] ]
>>> filtered_list = [[y for y in x if y not in disallowed] for x in my_list]
>>> print filtered_list
[[4, 5, 6, 7], [4, 5, 8, 8]]
You can actually delete from a list while you iterate over it, provided you to it backwards (so deletion only affects higher indices which you have already seen during this iteration):
data = [["A1","A2"],
["B1","B2"],
["C1","C2"]]
for i, pair in reversed(data):
if pair[0] == 'A1':
del data[i]

how do i write a list comprehension?

I want to write a list comprehension that will have print an element out of an array every other element.
How do I do so?
array = [1,2,3,4,5,6,7,8,9]
output: 2
3
4
5
6
7
8
9
array = [1,2,3,4,5,6,7,8,9]
newarray = [array[i] for i in range(0, len(array), 2)]
print(newarray)
The result is [1, 3, 5, 7, 9].
The question is a bit unclear as to what the final use of the code is, and therefore what the best way to accomplish it is. But if you are wanting to select every second element out of a list, you can just use slice notation:
>>> array = [1,2,3,4,5,6,7,8,9]
>>> odd = array[::2]
>>> odd
[1, 3, 5, 7, 9]
>>> even = array[1::2]
>>> even
[2, 4, 6, 8]
This can be generalised to selecting every nth element by changing the step parameter, e.g., to select every third element:
>>> third = array[::3]
>>> third
[1, 4, 7]
"an element out of an array every other element" .. meaning print out every other element?
If you really want to use list comprehension, here are two, one to collect the even index entries, the other the odd in their respective lists which are then printed out.
evens = [elem for i, elem in enumerate(array) if not (i % 2)]
odds = [elem for i, elem in enumerate(array) if i % 2]
print evens # entries at even index value
[1, 3, 5, 7, 9]
print odds # entries at odd index values
[2, 4, 6, 8]

Categories

Resources