Is there a way to be able to access specific sublist element?
For example:
list = [["a","b"],["c","d"]]
how to print out only b??
Thanks!
Firstly, it is a bad practice to name a variable as list as it is a keyword in Python. Say, it is changed to lis
lis = [["a","b"],["c","d"]]
To access b, you need to first get to the first element of lis, by lis[0]. This gives ["a", "b"]. Now you need to further go into it, so you do lis[0][1], This gives the 1-indexed element of lis[0], i.e., 'b'
You need to select the first sublist in your list (index 0), and then the second element of your sublist (index 1).
Result is list[0][1].
Related
I a have list like the one below:
['LDf12yiH3xpt4dtepYyqdw==', '802970-0', 'LIFETIME RENTS', 'LIFE ASSURANCE']
And I'm trying to get its values to assign them to other variables. However, when use for to loop it, I get the the char of each value according to given position.
For example, if I want to get the first element:
reg_0 = ['LDf12yiH3xpt4dtepYyqdw==', '802970-0', 'LIFETIME RENTS', 'LIFE ASSURANCE']
for i in reg_0:
print(i[0])
expected_output = 'LDf12yiH3xpt4dtepYyqdw=='
current_output = 'L 8 L L' # one char of each value, each line
Here is the solution to print just the first item.
print(reg_0[0])
Here is the solution to print each item.
for i in reg_0:
print(i)
You are printing the first element of each element of the list.
For i in reg_0: # is calling 'LDf12yiH3xpt4dtepYyqdw==' as i
and i[0] # is calling 'L'
and so on
for iterates over each element in the list. In your example, i will be the element in reg_0. If you want to iterate by index, you could do:
for i in range(0, len(reg_0)):
print(reg_0[i])
Or you could simply access the elements directly
for i in reg_0:
print(i)
What you are currently doing is:
for i in reg_0:
Here, you are iterating over the elements of the list. print(i[0]) is printing the first letter of the element because i is assigned an element from the list when it iterates and you are indexing the first element of i. So, here is some sample:
for i in reg_0:
print(i[0])
It is doing the following:
i='LDf12yiH3xpt4dtepYyqdw=='
i[0]='L' # printed
i='802970-0'
i[0]='8' # printed
i='LIFETIME RENTS'
i[0]='L' # printed
i='LIFE ASSURANCE'
i[0]='L' # printed
If you simply want the first element of the list, then do reg_0[0]
reg_0 = ['LDf12yiH3xpt4dtepYyqdw==', '802970-0', 'LIFETIME RENTS', 'LIFE ASSURANCE']
print(reg_0[0])
I have the following quick issue, hope you can help me.
I have a list my_list where there are like 900 elements and all the elements have the following format:
"###" + _ + "the name of the element"
Like this:
['123_rop','456_tuy','789_wqw',......]
How can I find the full element name with just the first 3 digits?
For example, I want to know the full name of the element that starts with "123".
The result must be:
result="123_rop"
The three first numbers of each element inside the list are never duplicated.
Try it:
lst = ['123_rop','456_tuy','789_wqw',"123_hhj"]
[i for i in lst if "123" in i]
def get_full_name(start_val)
new_list = [elem for elem in my_list if elem.statswith(start_val)]
return (new_list|'not found')
Actually, somehow this works
def check_by_start(num):
for i in list:
if i.startswith(num):
return i
print(check_by_start("887"))
but just with numbers, if I want to get an element that starts with letters it doesn't work
I am trying to iterate through list_a so that each index in list_a is checked against every index in list_b. I have read about how to check lists against each other by index, but so far only understand how to iterate through the lists at the same time.
for i in range(list_a)
if list_a[i] == list_b[i]
I considered using a while loop to use a single value of list_a and iterate through list_b
while i < len(list_b)
for list_a[0] == list_b[i]
for list_a[1] == list_b[i]
But if i write it like this then the index for a is manually determined and if it changes because the list it appended, which it needs to be elsewhere in my code, it will stop working. How can i use two separate counters so that i check each element of list_a against every element of list_b for every element of list_a. Could I use a counter?
The reason I want to write the code like this is to make sure I identify repeating elements in list_b and know every every location at which they exist.
If you want to check every element of list_a against every element in list_b you should use nested for
for el_a in list_a:
for el_b in list_b:
if el_a == el_b:
# do stuff
I'm looking to retrieve all but one value from a list:
ll = ['a','b','c']
nob = [x for x in ll if x !='b']
Is there any simpler, more pythonic way to do this, with sets perhaps?
given that the element is unique in the list, you can use list.index
i = l.index('b')
l = ll[:i] +ll[i+1:]
another possibility is to use list.remove
ll.remove('b') #notice that ll will change underneath here
whatever you do, you'll always have to step through the list and compare each element, which gets slow for long lists. However, using the index, you'll get the index of the first matching element and can operate with this alone, thus avoiding to step through the remainder of the list.
list_ = ['a', 'b', 'c']
list_.pop(1)
You can also use .pop, and pass the index column, or name, that you want to pop from the list. When you print the list you will see that it stores ['a', 'c'] and 'b' has been "popped" from it.
I have (result from a query) my_list = [('a',),('b',),('c',),('g',),('j',)]
I want to translate it to ['a','b','c']
What I have so far r = [rs for rs in my_list if rs not in[('g',),('j',)]]
This will fetch ('a',),('b',),('c',)
inputs = [('a',),('b',),('c',),('g',),('j',)]
r = [left for (left,) in inputs if left not in ['g','j']]
Be careful that list is an important function in python, using it as a variable name will override it.
You need to select the first element of the tuple:
r = [rs[0] for rs in list if rs not in[('g',),('j',)]]
# ^
I do not get what the rules are for selecting items, but you want to flatten your initial list (list renamed to l):
[item for sublist in l[:3] for item in sublist]
This returns ['a', 'b', 'c'].
In case you already know the structure of your input list, you do not need to filter each item. In case the filter rules are more complex that your current question suggests, you should specify them.