How to connect lists together


#1

hello guys ,
i have many lists in my python script , and i made for loop to search for a number , if the number is not in the first list , i move to next list , and so on , but the problem is how to connect all lists together in an order to search in them sequentially


#2

what do you what to know?

Which list the number is in? Which index?

  1. create a list of all the lists you want to check
  2. list.AllIndicesOf @L2

?


#3

if i have three lists
first = [1,3,6]
second = [8,10,12]
third = [14,16,18]
and i have two numbers so if the first number = 1 it will check in the first list if the second number is less than any number in the list , if not it moves to the next list , so if i have two numbers (1, 15) the first number is 1 so it will search in the first list then the second and the third list will be the true one , finally if the condition is true in the first list it will print “first” if in the second list it prints “second” and so on , so in this example it will print “three”


#4

This is pretty confusing. Can you show it in a graph with what the inputs would be (the first, second, third list structure) and what you want the output to be?


#5

something like this?


#6

Iam sorry , what i want to do is complicated , but in a simple way ,i want a way to make an array of arrays like if i have three lists

first = [1,3,6]
second = [8,10,12]
third = [14,16,18]

Is there a way to make array of all this arrays like
All = [first , second , third ]
So by make that “All[0]” i can access the elements of the first list


#7

So you mean the basic List.Create node?

Please show your Dynamo graph, as it will be easier for us to understand you after seeing what your work/outputs are like.


#8

Yes , exactly but if there is a way to do that inside a python “text” scripting it will be better


#9

Can you show the python then? It should be simple with the .append() method but I would need to see what your inputs/variables look like.


#10

Joining the lists directly in Python is actually worse in regards to managing your graph. If you use the List.Create node like Kenny showed, it would allow you to access all of your lists (regardless of how many there are) through a single node. If you have all of your lists as separate inputs in Python, you would have to open your script every time you change the number of lists. Here’s an example using the Python node where all inputs are lists:

The above option is only viable if all of your inputs are the lists you want to test.

Here is a modified strategy which will only work if all of the inputs after a specified index are lists:

Again, these are options if you want to do it in Python, but I would highly recommend using Kenny’s method instead.


#11

I dont have to go in my python script every time , i can make a number node as input to the python script and i want to go deep into programming and i want to turn it to add-in in the future
So simply , what i want to do that i have 3 fixed (unchangable) lists and one variable input(that input i type it manually) i want to search the input(a number ) through the lists (test if the number is equal to any number in the list) it will search the lists in an order and i want to make different outputs based on the list i found the number in it ( if i found the condition is true in the first list the ouput will be 20(for example ) ,if it in the second list the output will be 50) and so on


#12

This is slightly different from the scenario you described earlier in the thread. Here is an example addressing one of the earlier posts:

values_a = IN[0]
values_b_all = IN[1:4]
values_b_names = ['First', 'Second', 'Third']

results = []
for i in range(len(values_a)):
    value_a = values_a[i]
    for j in range(len(values_b_all)): # Assume same length for values_b_names as values_b_all
        values_b_name = values_b_names[j]
        values_b = values_b_all[j]
        if value_a in values_b:
            results.append(values_b_name)

OUT = results

List Test.dyn (11.2 KB)

You could modify this to instead output the numbers which were matched. For example:

if value_a in values_b:
    results.append(value_a)

#13

Like I asked in my first post, please show a Dynamo graph detailing what you want the inputs to be and what you want the expected output to be. It would help more than just having a text-based description.

cgartland’s script should work. If you want the output to stop after it finds the item in one of the lists (for example, value of 1 is in first and third, but only output first), this can work:

# The inputs to this node will be stored as a list in the IN variables.
testing = IN[0]
checkvals = IN[1:4]
checknames = ["first", "second", "third"]

outvals = []

# Place your code below this line
for val in testing:
    for i, check in enumerate(checkvals):
        if val in check:
            outvals.append(checknames[i])
            break

# Assign your output to the OUT variable.
OUT = outvals

What do you want it to show if it cannot find the value in any of the lists?


#14

Now that’s working


#15

I want to throw an exception if the input is out of a specific range